javascript - React not updating component props? - Stack Overflow

It seems that my ponent props are not updated the way I thought they ought to be. var AdditionalInfoBlo

It seems that my ponent props are not updated the way I thought they ought to be.

var AdditionalInfoBlock = React.createClass({

    getInitialState: function() {
        return ({
            ment: this.propsment
        });
    },

    onCommentChange: function(e) {
        this.setState({ment: e.target.value});
        this.props.onCommentChange(e);
    },

    render: function() {
        return (
            <ToolBlock>
                <div className="container">
                    <form>
                        <textarea value={this.propsment} onChange={this.onCommentChange} />
                    </form>
                </div>
            </ToolBlock>
        );
    }
};

var MainTool = React.createClass({

    getInitialState: function () {
        return {
            ment: undefined
        };
    },

    restart: function (e) {
        e && e.preventDefault && e.preventDefault();
        this.setState(this.getInitialState());
    },

    onCommentChange: function(e) {
        this.setState({
            ment: e.target.value
        });
    },

    render: function() {
        return (
            <div>
                <AdditionalInfoBlock ment={this.statement}
                                     onCommentChange={this.onCommentChange} />
            </div>
        );
    }
};

What I want this code to do is basically hold the ment's value until I post it and call restart - then it should reset the value in both AdditionalInfoBlock and MainTool. At the moment after restart is called when I console.log() the state of MainTool, the ment value is undefined. However, if I log AdditionalInfoBlock state and/or props, then the ment value is not reset in neither of those.

(PS. This is obviously a short version of the code, hopefully only incorporating the relevant bits. Although restart is not called in this excerpt, it doesn't mean that I have forgotten to call it at all :))

It seems that my ponent props are not updated the way I thought they ought to be.

var AdditionalInfoBlock = React.createClass({

    getInitialState: function() {
        return ({
            ment: this.props.ment
        });
    },

    onCommentChange: function(e) {
        this.setState({ment: e.target.value});
        this.props.onCommentChange(e);
    },

    render: function() {
        return (
            <ToolBlock>
                <div className="container">
                    <form>
                        <textarea value={this.props.ment} onChange={this.onCommentChange} />
                    </form>
                </div>
            </ToolBlock>
        );
    }
};

var MainTool = React.createClass({

    getInitialState: function () {
        return {
            ment: undefined
        };
    },

    restart: function (e) {
        e && e.preventDefault && e.preventDefault();
        this.setState(this.getInitialState());
    },

    onCommentChange: function(e) {
        this.setState({
            ment: e.target.value
        });
    },

    render: function() {
        return (
            <div>
                <AdditionalInfoBlock ment={this.state.ment}
                                     onCommentChange={this.onCommentChange} />
            </div>
        );
    }
};

What I want this code to do is basically hold the ment's value until I post it and call restart - then it should reset the value in both AdditionalInfoBlock and MainTool. At the moment after restart is called when I console.log() the state of MainTool, the ment value is undefined. However, if I log AdditionalInfoBlock state and/or props, then the ment value is not reset in neither of those.

(PS. This is obviously a short version of the code, hopefully only incorporating the relevant bits. Although restart is not called in this excerpt, it doesn't mean that I have forgotten to call it at all :))

Share Improve this question asked Dec 26, 2015 at 12:13 wanaryytelwanaryytel 3,5023 gold badges20 silver badges28 bronze badges 4
  • Try accessing the state in ponentDidUpdate() instead of accessing it exactly after your setState. And facebook.github.io/react/tips/… – oobgam Commented Dec 26, 2015 at 12:33
  • I'm not following entirely, could you elaborate on how that would solve my issue? The main problem is that in <textarea value={this.props.ment} onChange={this.onCommentChange} /> this.props.ment returns the previous prop value although a new one has been assigned in the parent object (should return undefined but returns whatever was there before calling restart). – wanaryytel Commented Dec 26, 2015 at 12:55
  • I just got confused with too many ponent handling the same state. I don't think the textbox value property accepts undefined as an argument, try setting the initialState into an empty string or in your restart() method, this.setState({ment: ''}); – oobgam Commented Dec 26, 2015 at 13:13
  • Tried that already before posting, but as stated in the original post, the problem lies elsewhere - console.log(this.state.props) does not return undefined either, but the previous value. – wanaryytel Commented Dec 26, 2015 at 13:21
Add a ment  | 

1 Answer 1

Reset to default 4

Since you're handling the same state on both ponents MainTool and AdditionalInfoBlock, finding value of the ment can get confusing. While you're listening for the ment change, you're setting the state on both ponents.

The changed state in your parent ponent MainTool is passing the props to the child AdditionalInfoBlock. getInitialState is invoked once before mounting (getInitialState documentation). Therefore, the passed on property is not handled by you child ponent on succeeding updates. By using ponentWillReceiveProps, you will be able to handle the props sent by MainTool.

var AdditionalInfoBlock = React.createClass({
    ...

    ponentWillReceiveProps(nextProps) {
            this.setState({ment: nextProps.ment});
    },

Working Code: https://jsfiddle/ta8y1w1m/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204895a4616545.html

相关推荐

  • javascript - React not updating component props? - Stack Overflow

    It seems that my ponent props are not updated the way I thought they ought to be. var AdditionalInfoBlo

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信