简体   繁体   English

React Native:孩子对父母道具的变化没有反应

[英]React Native: children not reacting to parent prop change

I have a custom modal component where I pass a isVisible prop, this prop comes from it's parent state property state, now in the modal (child component) I can eit and event and react to it in the parent, changing the value in the parent should effect the child and the modal should close now, but the modal is not closing!我有一个自定义模态组件,我在其中传递了一个 isVisible 道具,该道具来自它的父级 state 属性 state,现在在模态(子组件)中,我可以在父级中 eit 和事件并对其做出反应,更改父级中的值应该影响孩子并且模态现在应该关闭,但模态没有关闭!

Parent:家长:


const [ modalIsVisible, setModalIsVisible ] = useState(false);

const handleCloseModal = ()=>
    {
        setModalIsVisible(false); Alert.alert('caca');
    };

<ReviewFormModal1 onCloseModal={ ()=>{ handleCloseModal() } } isVisible={modalIsVisible}></ReviewFormModal1>

Child:孩子:


const onClosePress = () => 
    {
        props.onCloseModal();
    }

<Modal isVisible={props.isVisible} style={styles.modal}>
                <TouchableOpacity onPress={ ()=>{ onClosePress() } } style={{ flex:1 }}></TouchableOpacity>
</Modal>

I'm still a react-native newb (coming from vue which imo is much easier), but afaik this is the proper way to react to prop changes in the parent, props being always top-down.我仍然是 react-native 新手(来自 imo 更容易的 vue),但是 afaik 这是对父项中的道具更改做出反应的正确方法,道具始终是自上而下的。

Either remove handleCloseModal curly brackets or add a return statement:删除 handleCloseModal 大括号或添加 return 语句:

<ReviewFormModal1 onCloseModal={ () => handleCloseModal()  } isVisible={modalIsVisible}></ReviewFormModal1>

or或者

<ReviewFormModal1 onCloseModal={ ()=>{return handleCloseModal() } } isVisible={modalIsVisible}></ReviewFormModal1>

Also, you can pass a simple pointer for your function like this:此外,您可以像这样为 function 传递一个简单的指针:

<ReviewFormModal1 onCloseModal={handleCloseModal} isVisible={modalIsVisible}></ReviewFormModal1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM