如何从React +子参数中的子组件更新父状态

时间:2018-06-29 21:14:55

标签: reactjs

我正在遵循此tutorial,但没有说明如何向函数传递参数。

在他们拥有的孩子中

 <Button onClick={this.props.action} />



   handler(id) {
        this.setState({
            messageShown: true,
             id : id
        });
    }

如果我想随同发送一个值(例如一些ID或其他内容),会发生什么情况。

我试图做

 <Button onClick={() => this.props.action(1)} />

但是我的“状态”是不确定的。

3 个答案:

答案 0 :(得分:1)

要实现所需的功能,应在Child组件中调用一个调用传递函数的函数。在这种情况下,您将可以传递所需的任何参数。

让我们的代码!

您的父级组件将是:

class Parent extends React.Component {
    constructor(props) {
        super(props)

        // Bind the this context to the handler function
        this.handler = this.handler.bind(this);

        // Set some state
        this.state = {
            messageShown: false,
            id: -1 // initialize new state property with a value
        };
    }

    // This method will be sent to the child component
    handler(id) {
        this.setState({
            messageShown: true,
            id: id
        });
    }

    // Render the child component and set the action property with the handler as value
    render() {
        return <Child action={this.handler} />
    }
}

您的子级组件将是

class Child extends React.Component {
    render() {
        return (
            <div>
                {/* The button will execute the handler function set by the parent component, passing any parameter */}
                <Button onClick={() => this.props.action(1)} />
            </div>
        )
    }
}

希望这会有所帮助

答案 1 :(得分:0)

在没有看到完整代码示例的情况下很难说出问题所在,但是您尝试做的事情肯定是可能的。这是一个可行的示例。

class Parent extends React.Component {
    constructor(props) {
        super(props)

        // Bind the this context to the handler function
        this.handler = this.handler.bind(this);

        // Set some state
        this.state = {
            messageShown: false
        };
    }

    // This method will be sent to the child component
    handler(id) {
        this.setState({
            messageShown: true,
            id: id
        });
    }

    // Render the child component and set the action property with the handler as value
    render() {
        console.log(this.state);
        return (
           <div>
              <Child action={this.handler} />
              <div>{this.state.id}</div>
           </div>
        );
    }
}

class Child extends React.Component {
    render() {
        return (
            <div>
                {/* The button will execute the handler function set by the parent component */}
                <button onClick={() => this.props.action(1)} > button </button>
            </div>
        )
    }
}
ReactDOM.render(<Parent />, document.getElementById('main'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>


<div id="main"></div>

答案 2 :(得分:0)

通常,在调用回调函数后未定义this.state时,这是一个绑定问题。仔细检查处理程序函数是否已在父组件的构造函数中对此进行了绑定。

this.handler = this.handler.bind(this);

有关绑定的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

相关问题