【发布时间】:2020-07-10 19:43:30
【问题描述】:
我有一个子组件,我想用一个道具制作多个东西, 主要思想是我的子组件我有这个我想传递给父组件的项目,同时我想关闭模式“从父组件更改状态”。
那我该如何处理呢?
Const Child =(props) => {
const [itemSelected,setItemSelected] = useState(null);
const passData= ()=>{
....
// I want here to send item selected and call a function from parent
// props.onPress( itemSelected, and let parent to call a function )
}
return (
<Button onPress={passData} />
}
Const Parent = ()=>{
Const sendData = ()=>{
// Change parent state "close modal"
// get data from child and send it to Api
}
return (
<Child onPress={sendData} />
);
}
【问题讨论】:
-
在父级中 - 更新
sendData以接受参数itemSelected。在孩子中 - 从passData致电props.onPress(itemSelected)。 -
@himayan 感谢它的作品,你能解释一下吗?我为什么要通过
itemSelected传递props. onPress() -
当然。我已经发布了更详细的答案。我希望它有所帮助。 @Oliver D.
标签: javascript reactjs react-native