我正在尝试从子组件中打开模式

时间:2019-07-17 07:31:46

标签: react-native

我尝试了很多方法,但是这些方法都不奏效。我从5天开始尝试。 我用过redux,props,然后使用ref。这些都没有帮助。 当我从另一个类调用它时,我需要该模式是可见的。

// this is the parent class
export default class Search1 extends React.Component {
constructor(props) {
   super(props);
   this.setModalVisible1 = this.setModalVisible1.bind(this);
   this.state = {
     modalVisible1: false,
   };
   this.closeModal = this.closeModal.bind(this);
 }
   setModalVisible1(visible) {
       this.setState({ modalVisible1: visible });
       // this.setModalVisible2(visible);
   }
closeModal() {
   console.log("modalvi1 value is in closemodal ", this.state.modalVisible1);
   this.setState({ modalVisible1: false });
 }
   render() {
       return (
           {/* it renders the screen again when I call the */}
           <Modal
 closeModal={() => this.closeModal}
           animationType="slide"
           transparent={true}
           visible={this.state.modalVisible1}
           </Modal>
           <NavStack />
       );
   }
}
// this is the child class
class HomeScreen extends React.Component {
   render() {
<TouchableOpacity
               style={styles.firstStyle}
               onPress={() => {
                 this.props.closeModal();
                 );
               }}
             >
   return (

}

从外部子类调用时,模态应该可见。从父类调用时,它必须关闭。

我尝试使用redux。那行不通。然后我用了道具。然后我用了裁判。这些都没有帮助。只是对此感到厌倦。帮帮我。

1 个答案:

答案 0 :(得分:0)

将函数传递给模态

      closeModal() {
         this.setState({ modalVisible1: false });;
      }

                 <Modal
                  closeModal={this.closeModal}
                  animationType="slide"
                  transparent={true}
                  visible={this.state.modalVisible1} />

并以this.props.closeModal()

形式在子组件上运行
相关问题