How change a variable of a parent in react-native











up vote
0
down vote

favorite












I want to change client = {state:0}
You can access it by using this.client.state



I also have a child which contains a button.
I'm trying to change this 'state' variable when you press the button.



For some reason everything I find everything on the internet not working for me.
I've been stuck at it for 5 hours and I think it's time to ask help myself



import React from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native';
import Home from './Components/Home';
import Activity from './Components/Activity';

export default class App extends React.Component {
client = {state:0}

render() {
if(this.client.state == 0){
return(
<View style={{flex: 1}}>
<Home />
<Child />
</View>
);
} else {
return(
<View style={{flex: 1}}>
<Activity />
<Child />
</View>
);
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I want to change client = {state:0}
    You can access it by using this.client.state



    I also have a child which contains a button.
    I'm trying to change this 'state' variable when you press the button.



    For some reason everything I find everything on the internet not working for me.
    I've been stuck at it for 5 hours and I think it's time to ask help myself



    import React from 'react';
    import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native';
    import Home from './Components/Home';
    import Activity from './Components/Activity';

    export default class App extends React.Component {
    client = {state:0}

    render() {
    if(this.client.state == 0){
    return(
    <View style={{flex: 1}}>
    <Home />
    <Child />
    </View>
    );
    } else {
    return(
    <View style={{flex: 1}}>
    <Activity />
    <Child />
    </View>
    );
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to change client = {state:0}
      You can access it by using this.client.state



      I also have a child which contains a button.
      I'm trying to change this 'state' variable when you press the button.



      For some reason everything I find everything on the internet not working for me.
      I've been stuck at it for 5 hours and I think it's time to ask help myself



      import React from 'react';
      import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native';
      import Home from './Components/Home';
      import Activity from './Components/Activity';

      export default class App extends React.Component {
      client = {state:0}

      render() {
      if(this.client.state == 0){
      return(
      <View style={{flex: 1}}>
      <Home />
      <Child />
      </View>
      );
      } else {
      return(
      <View style={{flex: 1}}>
      <Activity />
      <Child />
      </View>
      );
      }









      share|improve this question













      I want to change client = {state:0}
      You can access it by using this.client.state



      I also have a child which contains a button.
      I'm trying to change this 'state' variable when you press the button.



      For some reason everything I find everything on the internet not working for me.
      I've been stuck at it for 5 hours and I think it's time to ask help myself



      import React from 'react';
      import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native';
      import Home from './Components/Home';
      import Activity from './Components/Activity';

      export default class App extends React.Component {
      client = {state:0}

      render() {
      if(this.client.state == 0){
      return(
      <View style={{flex: 1}}>
      <Home />
      <Child />
      </View>
      );
      } else {
      return(
      <View style={{flex: 1}}>
      <Activity />
      <Child />
      </View>
      );
      }






      react-native






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:36









      NoahS

      193




      193
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          There are different ways of doing this. It could be done with Redux for example, but let's take a simpler approach.



          Also note that it can't be done by props, because a child component cannot update its parents' props.



          Also note that the way you are using the state seems rather strange. It should be set on the class level (or component level).



          class MyComponent extends React.Component {
          constructor(props) {
          super(props);
          this.state = {myProperty: 0};
          }
          }


          You could pass a callback method to the Child React Component.



          <Child callback={this.onButtonClick} />



          On the Client Component, create that callback method:



          onButtonClick() {
          this.setState({buttonClicked: true});
          }


          To keep it clean, define the initial value in the constructor. You'll also have to bind the function to have a correct this parameter, otherwise the this variable will be from the event instead of the class you're expecting.



          constructor(props) {
          super(props);
          this.state = {buttonClicked: false};
          this.onButtonClick = this.onButtonClick.bind(this);
          }


          That's it for the Client component.



          Now on the Child Component, you'll need to trigger this callback method when possible.



          Imagine the Child has the following button, add an event handler on the child component as well, onChildButtonClick. You'll also have to bind in the constructor.



          constructor(props) {
          super(props);
          // bind this for access to this class' data
          this.onChildButtonClick = this.onChildButtonClick.bind(this);
          }

          onChildButtonClick() {
          // Might want to do a typeof is function check here too
          if (this.props.callback) {
          // Trigger the callback on the parent component, letting it know the button was triggered
          this.props.callback();
          }
          }

          render() {
          return <button onClick={this.onChildButtonClick}>Click me</button>;
          }


          During initialisation, the Parent component sends a callback method to the child component. Whenever the button is clicked on the child component, the child component triggers the function (callback) given by the parent, essentially running a piece of code on the parent component, which then updates the state with the requested value (could be a string, or anything).



          Redux



          Redux is another way of doing it, which basically keeps a sort of tracked database that can be used from any component, by pageload - however, that would require an entire tutorial.






          share|improve this answer





















            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',
            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%2f53250851%2fhow-change-a-variable-of-a-parent-in-react-native%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            There are different ways of doing this. It could be done with Redux for example, but let's take a simpler approach.



            Also note that it can't be done by props, because a child component cannot update its parents' props.



            Also note that the way you are using the state seems rather strange. It should be set on the class level (or component level).



            class MyComponent extends React.Component {
            constructor(props) {
            super(props);
            this.state = {myProperty: 0};
            }
            }


            You could pass a callback method to the Child React Component.



            <Child callback={this.onButtonClick} />



            On the Client Component, create that callback method:



            onButtonClick() {
            this.setState({buttonClicked: true});
            }


            To keep it clean, define the initial value in the constructor. You'll also have to bind the function to have a correct this parameter, otherwise the this variable will be from the event instead of the class you're expecting.



            constructor(props) {
            super(props);
            this.state = {buttonClicked: false};
            this.onButtonClick = this.onButtonClick.bind(this);
            }


            That's it for the Client component.



            Now on the Child Component, you'll need to trigger this callback method when possible.



            Imagine the Child has the following button, add an event handler on the child component as well, onChildButtonClick. You'll also have to bind in the constructor.



            constructor(props) {
            super(props);
            // bind this for access to this class' data
            this.onChildButtonClick = this.onChildButtonClick.bind(this);
            }

            onChildButtonClick() {
            // Might want to do a typeof is function check here too
            if (this.props.callback) {
            // Trigger the callback on the parent component, letting it know the button was triggered
            this.props.callback();
            }
            }

            render() {
            return <button onClick={this.onChildButtonClick}>Click me</button>;
            }


            During initialisation, the Parent component sends a callback method to the child component. Whenever the button is clicked on the child component, the child component triggers the function (callback) given by the parent, essentially running a piece of code on the parent component, which then updates the state with the requested value (could be a string, or anything).



            Redux



            Redux is another way of doing it, which basically keeps a sort of tracked database that can be used from any component, by pageload - however, that would require an entire tutorial.






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              There are different ways of doing this. It could be done with Redux for example, but let's take a simpler approach.



              Also note that it can't be done by props, because a child component cannot update its parents' props.



              Also note that the way you are using the state seems rather strange. It should be set on the class level (or component level).



              class MyComponent extends React.Component {
              constructor(props) {
              super(props);
              this.state = {myProperty: 0};
              }
              }


              You could pass a callback method to the Child React Component.



              <Child callback={this.onButtonClick} />



              On the Client Component, create that callback method:



              onButtonClick() {
              this.setState({buttonClicked: true});
              }


              To keep it clean, define the initial value in the constructor. You'll also have to bind the function to have a correct this parameter, otherwise the this variable will be from the event instead of the class you're expecting.



              constructor(props) {
              super(props);
              this.state = {buttonClicked: false};
              this.onButtonClick = this.onButtonClick.bind(this);
              }


              That's it for the Client component.



              Now on the Child Component, you'll need to trigger this callback method when possible.



              Imagine the Child has the following button, add an event handler on the child component as well, onChildButtonClick. You'll also have to bind in the constructor.



              constructor(props) {
              super(props);
              // bind this for access to this class' data
              this.onChildButtonClick = this.onChildButtonClick.bind(this);
              }

              onChildButtonClick() {
              // Might want to do a typeof is function check here too
              if (this.props.callback) {
              // Trigger the callback on the parent component, letting it know the button was triggered
              this.props.callback();
              }
              }

              render() {
              return <button onClick={this.onChildButtonClick}>Click me</button>;
              }


              During initialisation, the Parent component sends a callback method to the child component. Whenever the button is clicked on the child component, the child component triggers the function (callback) given by the parent, essentially running a piece of code on the parent component, which then updates the state with the requested value (could be a string, or anything).



              Redux



              Redux is another way of doing it, which basically keeps a sort of tracked database that can be used from any component, by pageload - however, that would require an entire tutorial.






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                There are different ways of doing this. It could be done with Redux for example, but let's take a simpler approach.



                Also note that it can't be done by props, because a child component cannot update its parents' props.



                Also note that the way you are using the state seems rather strange. It should be set on the class level (or component level).



                class MyComponent extends React.Component {
                constructor(props) {
                super(props);
                this.state = {myProperty: 0};
                }
                }


                You could pass a callback method to the Child React Component.



                <Child callback={this.onButtonClick} />



                On the Client Component, create that callback method:



                onButtonClick() {
                this.setState({buttonClicked: true});
                }


                To keep it clean, define the initial value in the constructor. You'll also have to bind the function to have a correct this parameter, otherwise the this variable will be from the event instead of the class you're expecting.



                constructor(props) {
                super(props);
                this.state = {buttonClicked: false};
                this.onButtonClick = this.onButtonClick.bind(this);
                }


                That's it for the Client component.



                Now on the Child Component, you'll need to trigger this callback method when possible.



                Imagine the Child has the following button, add an event handler on the child component as well, onChildButtonClick. You'll also have to bind in the constructor.



                constructor(props) {
                super(props);
                // bind this for access to this class' data
                this.onChildButtonClick = this.onChildButtonClick.bind(this);
                }

                onChildButtonClick() {
                // Might want to do a typeof is function check here too
                if (this.props.callback) {
                // Trigger the callback on the parent component, letting it know the button was triggered
                this.props.callback();
                }
                }

                render() {
                return <button onClick={this.onChildButtonClick}>Click me</button>;
                }


                During initialisation, the Parent component sends a callback method to the child component. Whenever the button is clicked on the child component, the child component triggers the function (callback) given by the parent, essentially running a piece of code on the parent component, which then updates the state with the requested value (could be a string, or anything).



                Redux



                Redux is another way of doing it, which basically keeps a sort of tracked database that can be used from any component, by pageload - however, that would require an entire tutorial.






                share|improve this answer












                There are different ways of doing this. It could be done with Redux for example, but let's take a simpler approach.



                Also note that it can't be done by props, because a child component cannot update its parents' props.



                Also note that the way you are using the state seems rather strange. It should be set on the class level (or component level).



                class MyComponent extends React.Component {
                constructor(props) {
                super(props);
                this.state = {myProperty: 0};
                }
                }


                You could pass a callback method to the Child React Component.



                <Child callback={this.onButtonClick} />



                On the Client Component, create that callback method:



                onButtonClick() {
                this.setState({buttonClicked: true});
                }


                To keep it clean, define the initial value in the constructor. You'll also have to bind the function to have a correct this parameter, otherwise the this variable will be from the event instead of the class you're expecting.



                constructor(props) {
                super(props);
                this.state = {buttonClicked: false};
                this.onButtonClick = this.onButtonClick.bind(this);
                }


                That's it for the Client component.



                Now on the Child Component, you'll need to trigger this callback method when possible.



                Imagine the Child has the following button, add an event handler on the child component as well, onChildButtonClick. You'll also have to bind in the constructor.



                constructor(props) {
                super(props);
                // bind this for access to this class' data
                this.onChildButtonClick = this.onChildButtonClick.bind(this);
                }

                onChildButtonClick() {
                // Might want to do a typeof is function check here too
                if (this.props.callback) {
                // Trigger the callback on the parent component, letting it know the button was triggered
                this.props.callback();
                }
                }

                render() {
                return <button onClick={this.onChildButtonClick}>Click me</button>;
                }


                During initialisation, the Parent component sends a callback method to the child component. Whenever the button is clicked on the child component, the child component triggers the function (callback) given by the parent, essentially running a piece of code on the parent component, which then updates the state with the requested value (could be a string, or anything).



                Redux



                Redux is another way of doing it, which basically keeps a sort of tracked database that can be used from any component, by pageload - however, that would require an entire tutorial.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 19:00









                Kevin Van Ryckegem

                96421238




                96421238






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53250851%2fhow-change-a-variable-of-a-parent-in-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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python