React (2) - TypeScript 에서 setState props로 넘길 때 Type 지정하는 법
2022. 4. 4. 23:30ㆍfront-end/React
728x90
상위 컴포넌트에서 useState 함수를 넘길 때 아래 처럼 기존에 해왔던 것처럼 void로 넘겼는데, react 에서 더 적절한 타입을 제공하고 있었다
interface ITestProps{
setTest: () => void
}
SetStateAction 과 Dispatch 이다.
import React, { SetStateAction, Dispatch } from 'react'
interface Props {
setTest: Dispatch<SetStateAction<boolean>>
}
위에 코드처럼 타입을 작성하게 되면 어떤 타입의 값을 넣어야하는지 훨씬 쉽게 알 수 있다.
https://newbedev.com/passing-usestate-as-props-in-typescript
Passing useState as props in typescript
The type that would match the function returned from invoking useState would be: setMyVar: (value: boolean | ((prevVar: boolean) => boolean)) => void; If we loo
newbedev.com
728x90
'front-end > React' 카테고리의 다른 글
React (3) - TypeScript 오늘 하루 안보기 팝업 만들기 (2) | 2022.04.10 |
---|---|
React (1) - redux-saga (0) | 2022.04.03 |