1

I have a small problem where I have parent class and child class. I want to modify the state that was initialized in parent class so that I can see updated state in parent class. Here's the code:

var Parent = React.createClass({
    getInitialState: function(){
        return{
           my_value: 0
        }
    },

    _increaseValue: function(){
        this.state.my_value++;
    },

    render: function(){
        return(
            <div><Child /></div>
        )
    }
});

var Child = React.createClass({
    render: function(){
        //at button I want to access _increaseValue function of parent
        return (
            <div>
                 <button onClick={_increaseValue}>Increase</button>
            </div>
        );
    }
});

Now when user clicks the button in child class I would like to get the updated my_value in parent class, thus my questions are:

  1. Is it possible?
  2. If yes, how it is done?
  3. Is this good practice or no?

2 Answers 2

2
  1. Is it possible?

    yes, it is possible

  2. If yes, how it is done?

    you can pass parent method to child through props, like so

    var Parent = React.createClass({
      getInitialState: function(){
        return {
          my_value: 0
        }
      },
    
      onChangeValue: function () {
        var value = this.state.my_value + 1;
    
        this.setState({
          my_value: value
        })
      },
    
      render: function() {
        return <div>
          <Child 
            onChangeValue={ this.onChangeValue } 
            value={ this.state.my_value } 
          />
        </div>;
      }
    });
    
    var Child = React.createClass({
      _handleClick: function(){
        this.props.onChangeValue();
      },
    
      render: function(){
        return <div>
          <h1> { this.props.value  } </h1>
          <button onClick={ this._handleClick }>Increase</button>
        </div>
      }
    });
    

    Example

  3. Is this good practice or no?

    It is good practice

Sign up to request clarification or add additional context in comments.

2 Comments

@Amir Al could you create fiddle with code where you use props?
@Amir Al ok, I understood
0

You need to pass function via props into your child component. And when you need to change you call this function. It is normal practice and react way.

Example:

var Parent = React.createClass({
    getInitialState: function(){
        return{
           my_value: 0
        }
    },

    onChildClick: function() {
        this.setState({
          my_value: this.state.my_value + 1
        })
    },

    render: function(){
        return(
            <div>
              {this.state.my_value}
              <Child onClick={this.onChildClick.bind(this)}/>
            </div>
        )
    }
});

var Child = React.createClass({
    _handleClick: function(){
        this.props.onClick();
    },

    render: function(){
        return (
            <div>
                 <button onClick={this._handleClick}>Increase</button>
            </div>
        );
    }
});

Example on JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.