Type error: Type 'Dispatch < SetStateAction>' is not assignable to > type '() => void'. TS2322
...중략...
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
...중략...
<Route
path="/test"
element={
<Test
title="Login"
setEmail={setEmail}
setPassword={setPassword}
handleAction={() => handleAction(1)}
/>
}
/>
interface lProps {
title: string;
setEmail: () => void;
setPassword: () => void;
handleAction: () => void;
}
다음의 내용이 생각났습니다.
함수(setEmail,setPassword,handleAction)을 보내기 때문에, 자식에서 props의 타입을 함수 타입(()=> void)으로 정의해야겠다는 점
email, password, handleAction에 매개변수로 들어갈 값이,
처음에는 값이 없기 때문에,
즉 null,undefined이기 때문에,
전달 할 값이 없어, 비어있는 형태로 보내면 된다 생각한 점
const [user,setUser] = useState('')
=> 리액트는, 타입스크립트는 setUser함수를 보면 "user는 어떻게 할껀데" 라고 생각한다
리액트 타입스크립트에서는 set00함수의 타입을 "Dispatch"라고 정의합니다.
type Dispatch = (value: A) => void;
즉 전달하는 매개변수의 타입을 정의해야 합니다.
const [user, setUser] = useState('')
setUser함수는 user를 (매개변수로) 이용해 새로운 값으로 "set" 하는 함수입니다.
즉, user를 사용하기 때문에, 어떤 user의 타입을 전달하는것인지 정의해야합니다.
https://stackoverflow.com/questions/54575523/issue-with-passing-hook-to-child-typescript
여기를 클릭하면 이동합니다.
코드는 Router.tsx -> Test.tsx-> Form.tsx 순으로 함수 props를 전달하고 있습니다.
...중략...
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
...중략...
<Route
path="/test"
element={
<Test
title="Login"
setEmail={setEmail}
setPassword={setPassword}
handleAction={() => handleAction(1)}
/>
}
/>
import Form from '../Components/Common/Form';
interface lProps {
title: string;
setEmail: () => void;
setPassword: () => void;
handleAction: () => void;
}
const Test = ({ title, setEmail, setPassword, handleAction }: lProps) => {
return (
<div>
<Form
title={title}
setEmail={setEmail}
setPassword={setPassword}
handleAction={handleAction}
/>
</div>
);
};
export default Test;
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';
import Button from './Button';
export default function BasicTextFields({
title,
setPassword,
setEmail,
handleAction,
}) {
return (
<div>
...중략 ...
</div>
);
}
자식 컴포넌트의 인터페이스, lProps
interface lProps {
title: string;
setEmail: () => void;
setPassword: () => void;
handleAction: () => void;
}
interface lProps {
title: string;
setEmail: (email: string) => void;
setPassword: (password: string) => void;
handleAction: (id: number) => void;
}
자식, Test.tsx에서 props로 받는 함수의 매개변수의 타입을 지정했습니다.
setEmail이란 함수는 email을 value를 set하는데, 이용되는 email의 타입을 string으로,
setPassword에서 password는 string으로,
handleAction에서 id는 number로,