简体   繁体   English

在ReactJS中从子级访问父类方法

[英]Access Parent Class Method from Child in ReactJS

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: 现在,当用户单击子类中的按钮时,我想在父类中获取更新的my_value ,因此我的问题是:

  1. Is it possible? 可能吗?
  2. If yes, how it is done? 如果是,如何完成?
  3. Is this good practice or no? 这是好习惯吗?
  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 你可以通过传递父类的方法,以儿童props ,像这样

     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 这是一个好习惯

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 JSFiddle上的示例

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

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