【发布时间】:2023-01-09 18:09:00
【问题描述】:
在这种情况下,如何从子组件使用setDisabled?
const ChildButton = () => {
const [disabled, setDisabled] = useState(false);
return <div onClick={disabled ? null : console.log('do something')} />
}
从 './ChildButton' 导入 ChildButton;
const Parent = () => {
const controllSetStateFun = () => {
// use the child coomponent setDisabled then I can re-render child component from Parent
};
return (
<div>
<ChildButton />
</div>
);
}
【问题讨论】:
-
通常你要做的是将
useState放在父组件中,然后将disabled和setDisabled作为 props 发送给子组件。
标签: reactjs