我正在寻找将数据从子组件传递给其父组件的最简单的解决方案。
我听说过使用上下文,传递槽属性或更新道具,但我不知道哪一个是最好的解决方案。
我正在构建一个管理界面,包含一个 PageComponent,其中包含一个 ChildComponent 和一个表,我可以在其中选择多行。我想发送给我的父网页组件的行数,我已经选择了我的儿童组件。
差不多是这样:
网页内容:
<div className="App">
<EnhancedTable />
<h2>count 0</h2>
(count should be updated from child)
</div>
儿童组成部分:
const EnhancedTable = () => {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Click me {count}
</button>
)
};
我相信这是一个非常简单的事情做,我不想使用 redux。