Closing modal in child component React Native





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have two components in react native and I'm unable to close a modal from my child component.



ListTrips - Parent



ModalAddTrip - Child



ListTrips.js



import ModalAddTrip from './ModalAddTrip';
....
....
this.state = {
isModalAddTripVisible: false
}
....
handleDismissModalAddTrip = () => {
this.setState({ isModalAddTripVisible: false });
};

closeModal() {
this.refs.ModalAdd.handleDismissModalAddTrip();
}
....

<ModalAddTrip
ref="ModalAdd"
isVisible={this.state.isModalAddTripVisible}
onBackDropPress={this.handleDismissModalAddTrip}
closeModal={() => this.closeModal()}
onRequestClose={this.handleDismissModalAddTrip}
/>


ModalAddTrip.js



<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
closeModal={this.props.child}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this.closeModal}
>


I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal; along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.



Any help is greatly appreciated.
Thanks










share|improve this question































    0















    I have two components in react native and I'm unable to close a modal from my child component.



    ListTrips - Parent



    ModalAddTrip - Child



    ListTrips.js



    import ModalAddTrip from './ModalAddTrip';
    ....
    ....
    this.state = {
    isModalAddTripVisible: false
    }
    ....
    handleDismissModalAddTrip = () => {
    this.setState({ isModalAddTripVisible: false });
    };

    closeModal() {
    this.refs.ModalAdd.handleDismissModalAddTrip();
    }
    ....

    <ModalAddTrip
    ref="ModalAdd"
    isVisible={this.state.isModalAddTripVisible}
    onBackDropPress={this.handleDismissModalAddTrip}
    closeModal={() => this.closeModal()}
    onRequestClose={this.handleDismissModalAddTrip}
    />


    ModalAddTrip.js



    <Modal
    isVisible={isVisible}
    onBackdropPress={onBackDropPress}
    closeModal={this.props.child}
    >
    <Button
    style={{ fontSize: 18, color: 'white' }}
    containerStyle={{
    padding: 8,
    marginLeft: 70,
    }}
    onPress={this.closeModal}
    >


    I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal; along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.



    Any help is greatly appreciated.
    Thanks










    share|improve this question



























      0












      0








      0








      I have two components in react native and I'm unable to close a modal from my child component.



      ListTrips - Parent



      ModalAddTrip - Child



      ListTrips.js



      import ModalAddTrip from './ModalAddTrip';
      ....
      ....
      this.state = {
      isModalAddTripVisible: false
      }
      ....
      handleDismissModalAddTrip = () => {
      this.setState({ isModalAddTripVisible: false });
      };

      closeModal() {
      this.refs.ModalAdd.handleDismissModalAddTrip();
      }
      ....

      <ModalAddTrip
      ref="ModalAdd"
      isVisible={this.state.isModalAddTripVisible}
      onBackDropPress={this.handleDismissModalAddTrip}
      closeModal={() => this.closeModal()}
      onRequestClose={this.handleDismissModalAddTrip}
      />


      ModalAddTrip.js



      <Modal
      isVisible={isVisible}
      onBackdropPress={onBackDropPress}
      closeModal={this.props.child}
      >
      <Button
      style={{ fontSize: 18, color: 'white' }}
      containerStyle={{
      padding: 8,
      marginLeft: 70,
      }}
      onPress={this.closeModal}
      >


      I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal; along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.



      Any help is greatly appreciated.
      Thanks










      share|improve this question
















      I have two components in react native and I'm unable to close a modal from my child component.



      ListTrips - Parent



      ModalAddTrip - Child



      ListTrips.js



      import ModalAddTrip from './ModalAddTrip';
      ....
      ....
      this.state = {
      isModalAddTripVisible: false
      }
      ....
      handleDismissModalAddTrip = () => {
      this.setState({ isModalAddTripVisible: false });
      };

      closeModal() {
      this.refs.ModalAdd.handleDismissModalAddTrip();
      }
      ....

      <ModalAddTrip
      ref="ModalAdd"
      isVisible={this.state.isModalAddTripVisible}
      onBackDropPress={this.handleDismissModalAddTrip}
      closeModal={() => this.closeModal()}
      onRequestClose={this.handleDismissModalAddTrip}
      />


      ModalAddTrip.js



      <Modal
      isVisible={isVisible}
      onBackdropPress={onBackDropPress}
      closeModal={this.props.child}
      >
      <Button
      style={{ fontSize: 18, color: 'white' }}
      containerStyle={{
      padding: 8,
      marginLeft: 70,
      }}
      onPress={this.closeModal}
      >


      I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal; along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.



      Any help is greatly appreciated.
      Thanks







      react-native modal-dialog ref react-props






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 21:33









      Remul

      1,3551414




      1,3551414










      asked Nov 23 '18 at 16:01









      Oscar FenOscar Fen

      1217




      1217
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Here is a solution which I use to handle modal.



          export default class MyModal extends React.Component<Props, State>{

          constructor(props: Props){
          super(props);
          this.state = {
          visible: false,
          }
          }

          // Use this method to toggle the modal !
          toggleModal = () => {
          this.setState({visible: !this.state.visible});
          }


          render(){
          return(
          <Modal
          isVisible={this.state.visible}
          hideModalContentWhileAnimating
          onBackdropPress={() => {
          this.toggleModal();
          }}
          useNativeDriver
          >
          <View style={{backgroundColor: "white", padding: 5}}>
          </View>
          </Modal>
          );
          }
          }


          If I press behind it, the modal will close -> it can close itself.



          Now to manage it from the parent component just get a ref from your modal :



            <MyModal 
          ref={ref => {
          this.myModal = ref;
          }}
          />


          And you can toggle it from the parent component :



          toggleMyModal = () => {
          if(this.myModal){
          this.myModal.toggleModal();
          }
          }


          Let me know if you got it working :)






          share|improve this answer
























          • It worked thanks!

            – Oscar Fen
            Nov 25 '18 at 15:51











          • No problem @OscarFen ! Please consider accepting the answer as solution ;)

            – Hugo Laplace
            Nov 25 '18 at 16:39











          • I don't have enough reputation :(

            – Oscar Fen
            Nov 25 '18 at 16:49











          • You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

            – Hugo Laplace
            Nov 25 '18 at 16:53



















          0














          ModalAddTrip.js



            _toggleModal = () =>
          this.setState({ isVisible: !this.state.isVisible });
          ....

          render() {
          ....
          <Modal
          isVisible={isVisible}
          onBackdropPress={onBackDropPress}
          >
          <Button
          style={{ fontSize: 18, color: 'white' }}
          containerStyle={{
          padding: 8,
          marginLeft: 70,
          }}
          onPress={this._toggleModal} >
          </Modal>





          share|improve this answer
























          • didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

            – Oscar Fen
            Nov 23 '18 at 17:42














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449752%2fclosing-modal-in-child-component-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Here is a solution which I use to handle modal.



          export default class MyModal extends React.Component<Props, State>{

          constructor(props: Props){
          super(props);
          this.state = {
          visible: false,
          }
          }

          // Use this method to toggle the modal !
          toggleModal = () => {
          this.setState({visible: !this.state.visible});
          }


          render(){
          return(
          <Modal
          isVisible={this.state.visible}
          hideModalContentWhileAnimating
          onBackdropPress={() => {
          this.toggleModal();
          }}
          useNativeDriver
          >
          <View style={{backgroundColor: "white", padding: 5}}>
          </View>
          </Modal>
          );
          }
          }


          If I press behind it, the modal will close -> it can close itself.



          Now to manage it from the parent component just get a ref from your modal :



            <MyModal 
          ref={ref => {
          this.myModal = ref;
          }}
          />


          And you can toggle it from the parent component :



          toggleMyModal = () => {
          if(this.myModal){
          this.myModal.toggleModal();
          }
          }


          Let me know if you got it working :)






          share|improve this answer
























          • It worked thanks!

            – Oscar Fen
            Nov 25 '18 at 15:51











          • No problem @OscarFen ! Please consider accepting the answer as solution ;)

            – Hugo Laplace
            Nov 25 '18 at 16:39











          • I don't have enough reputation :(

            – Oscar Fen
            Nov 25 '18 at 16:49











          • You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

            – Hugo Laplace
            Nov 25 '18 at 16:53
















          0














          Here is a solution which I use to handle modal.



          export default class MyModal extends React.Component<Props, State>{

          constructor(props: Props){
          super(props);
          this.state = {
          visible: false,
          }
          }

          // Use this method to toggle the modal !
          toggleModal = () => {
          this.setState({visible: !this.state.visible});
          }


          render(){
          return(
          <Modal
          isVisible={this.state.visible}
          hideModalContentWhileAnimating
          onBackdropPress={() => {
          this.toggleModal();
          }}
          useNativeDriver
          >
          <View style={{backgroundColor: "white", padding: 5}}>
          </View>
          </Modal>
          );
          }
          }


          If I press behind it, the modal will close -> it can close itself.



          Now to manage it from the parent component just get a ref from your modal :



            <MyModal 
          ref={ref => {
          this.myModal = ref;
          }}
          />


          And you can toggle it from the parent component :



          toggleMyModal = () => {
          if(this.myModal){
          this.myModal.toggleModal();
          }
          }


          Let me know if you got it working :)






          share|improve this answer
























          • It worked thanks!

            – Oscar Fen
            Nov 25 '18 at 15:51











          • No problem @OscarFen ! Please consider accepting the answer as solution ;)

            – Hugo Laplace
            Nov 25 '18 at 16:39











          • I don't have enough reputation :(

            – Oscar Fen
            Nov 25 '18 at 16:49











          • You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

            – Hugo Laplace
            Nov 25 '18 at 16:53














          0












          0








          0







          Here is a solution which I use to handle modal.



          export default class MyModal extends React.Component<Props, State>{

          constructor(props: Props){
          super(props);
          this.state = {
          visible: false,
          }
          }

          // Use this method to toggle the modal !
          toggleModal = () => {
          this.setState({visible: !this.state.visible});
          }


          render(){
          return(
          <Modal
          isVisible={this.state.visible}
          hideModalContentWhileAnimating
          onBackdropPress={() => {
          this.toggleModal();
          }}
          useNativeDriver
          >
          <View style={{backgroundColor: "white", padding: 5}}>
          </View>
          </Modal>
          );
          }
          }


          If I press behind it, the modal will close -> it can close itself.



          Now to manage it from the parent component just get a ref from your modal :



            <MyModal 
          ref={ref => {
          this.myModal = ref;
          }}
          />


          And you can toggle it from the parent component :



          toggleMyModal = () => {
          if(this.myModal){
          this.myModal.toggleModal();
          }
          }


          Let me know if you got it working :)






          share|improve this answer













          Here is a solution which I use to handle modal.



          export default class MyModal extends React.Component<Props, State>{

          constructor(props: Props){
          super(props);
          this.state = {
          visible: false,
          }
          }

          // Use this method to toggle the modal !
          toggleModal = () => {
          this.setState({visible: !this.state.visible});
          }


          render(){
          return(
          <Modal
          isVisible={this.state.visible}
          hideModalContentWhileAnimating
          onBackdropPress={() => {
          this.toggleModal();
          }}
          useNativeDriver
          >
          <View style={{backgroundColor: "white", padding: 5}}>
          </View>
          </Modal>
          );
          }
          }


          If I press behind it, the modal will close -> it can close itself.



          Now to manage it from the parent component just get a ref from your modal :



            <MyModal 
          ref={ref => {
          this.myModal = ref;
          }}
          />


          And you can toggle it from the parent component :



          toggleMyModal = () => {
          if(this.myModal){
          this.myModal.toggleModal();
          }
          }


          Let me know if you got it working :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 22:11









          Hugo LaplaceHugo Laplace

          18010




          18010













          • It worked thanks!

            – Oscar Fen
            Nov 25 '18 at 15:51











          • No problem @OscarFen ! Please consider accepting the answer as solution ;)

            – Hugo Laplace
            Nov 25 '18 at 16:39











          • I don't have enough reputation :(

            – Oscar Fen
            Nov 25 '18 at 16:49











          • You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

            – Hugo Laplace
            Nov 25 '18 at 16:53



















          • It worked thanks!

            – Oscar Fen
            Nov 25 '18 at 15:51











          • No problem @OscarFen ! Please consider accepting the answer as solution ;)

            – Hugo Laplace
            Nov 25 '18 at 16:39











          • I don't have enough reputation :(

            – Oscar Fen
            Nov 25 '18 at 16:49











          • You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

            – Hugo Laplace
            Nov 25 '18 at 16:53

















          It worked thanks!

          – Oscar Fen
          Nov 25 '18 at 15:51





          It worked thanks!

          – Oscar Fen
          Nov 25 '18 at 15:51













          No problem @OscarFen ! Please consider accepting the answer as solution ;)

          – Hugo Laplace
          Nov 25 '18 at 16:39





          No problem @OscarFen ! Please consider accepting the answer as solution ;)

          – Hugo Laplace
          Nov 25 '18 at 16:39













          I don't have enough reputation :(

          – Oscar Fen
          Nov 25 '18 at 16:49





          I don't have enough reputation :(

          – Oscar Fen
          Nov 25 '18 at 16:49













          You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

          – Hugo Laplace
          Nov 25 '18 at 16:53





          You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project

          – Hugo Laplace
          Nov 25 '18 at 16:53













          0














          ModalAddTrip.js



            _toggleModal = () =>
          this.setState({ isVisible: !this.state.isVisible });
          ....

          render() {
          ....
          <Modal
          isVisible={isVisible}
          onBackdropPress={onBackDropPress}
          >
          <Button
          style={{ fontSize: 18, color: 'white' }}
          containerStyle={{
          padding: 8,
          marginLeft: 70,
          }}
          onPress={this._toggleModal} >
          </Modal>





          share|improve this answer
























          • didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

            – Oscar Fen
            Nov 23 '18 at 17:42


















          0














          ModalAddTrip.js



            _toggleModal = () =>
          this.setState({ isVisible: !this.state.isVisible });
          ....

          render() {
          ....
          <Modal
          isVisible={isVisible}
          onBackdropPress={onBackDropPress}
          >
          <Button
          style={{ fontSize: 18, color: 'white' }}
          containerStyle={{
          padding: 8,
          marginLeft: 70,
          }}
          onPress={this._toggleModal} >
          </Modal>





          share|improve this answer
























          • didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

            – Oscar Fen
            Nov 23 '18 at 17:42
















          0












          0








          0







          ModalAddTrip.js



            _toggleModal = () =>
          this.setState({ isVisible: !this.state.isVisible });
          ....

          render() {
          ....
          <Modal
          isVisible={isVisible}
          onBackdropPress={onBackDropPress}
          >
          <Button
          style={{ fontSize: 18, color: 'white' }}
          containerStyle={{
          padding: 8,
          marginLeft: 70,
          }}
          onPress={this._toggleModal} >
          </Modal>





          share|improve this answer













          ModalAddTrip.js



            _toggleModal = () =>
          this.setState({ isVisible: !this.state.isVisible });
          ....

          render() {
          ....
          <Modal
          isVisible={isVisible}
          onBackdropPress={onBackDropPress}
          >
          <Button
          style={{ fontSize: 18, color: 'white' }}
          containerStyle={{
          padding: 8,
          marginLeft: 70,
          }}
          onPress={this._toggleModal} >
          </Modal>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 16:21









          Selmi KarimSelmi Karim

          613313




          613313













          • didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

            – Oscar Fen
            Nov 23 '18 at 17:42





















          • didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

            – Oscar Fen
            Nov 23 '18 at 17:42



















          didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

          – Oscar Fen
          Nov 23 '18 at 17:42







          didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?

          – Oscar Fen
          Nov 23 '18 at 17:42




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449752%2fclosing-modal-in-child-component-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]