在使用typescript中,遇到父子组件传方法时,类型不匹配,记录下如何写类型。
父组件
export default function Learn() {
const [wordCount, setWordCount] = useState(0);
return (
<Box>
<Appbar setWordCount={setWordCount} />
<Link to="/">首页</Link>
learn
</Box>
);
}
子组件
export interface MyAppBarProps {
setWordCount: (x: number) => void;
}
function Myappbar(props: MyAppBarProps) {
const { setWordCount } = props;
}