【发布时间】:2023-01-19 05:32:19
【问题描述】:
我正在尝试将“section”和“setSection”从父母传递给孩子。
我正在关注这个,但它对我不起作用: Passing useState as props in typescript
错误: 未处理的运行时错误 类型错误:setSection 不是一个函数
父组件:
export default function CareerProgression() {
const [section, setSection] = useState< 'video' | 'condensed' | 'full' >('condensed');
return (
<ModuleToggle_withState section={section} setSection={setSection} />
);
}
孩子:
import { Dispatch, useEffect, SetStateAction } from 'react';
export function ModuleToggle_withState(section:any, setSection:Dispatch<SetStateAction<any>>) {
// Currently I'm using "any" just so I can test passing useState props
return (
<>
<SegmentedControl
defaultValue='video'
value={section}
onChange={(value: 'video' | 'condensed' | 'full') => setSection(value)}
/>
</>
);
}
【问题讨论】:
标签: reactjs typescript next.js