对于 React 中的一些 prop 命名,我在 VS Code 编辑器中收到此 Typescript 警告/错误。
Props must be serializable for components in the "use client" entry file, "setShow" is invalid.ts(71007)
例如:
const ComponentName = ({ show, setShow }: IComponentName) => {
...
Component logic
...
}
其中,
setShow
是更新状态的函数,从parent
传递到child
。
但是项目构建成功了。
为什么TS会对道具上的某些命名发出警告?名称
setShow
作为道具名称有什么问题?如何让 TS 不针对这些命名抛出警告/错误?
这对我有用,用普通函数替换箭头函数
❌代替:
type Props = {
onFinish: (status: string) => void;
};
✅ 使用这个:
type Props = {
onFinish(status: string): void;
};