react icon indicating copy to clipboard operation
react copied to clipboard

Why my page's state changed in some case, while I didn't use setState.

Open ip192 opened this issue 9 years ago • 4 comments

I create Class Message in a Message.jsx file, and its component TestInput behind it.

export default class Message extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            text: [],
        };
        this.showText = this.showText.bind(this);
    }
    showText() {
        console.log(this.state.text);
    }
    render() {
        return(
            <div>
                <TestInput text={this.state.text}/>
                <button className="btn btn-primary btn-lg" onClick={this.showText}>show click result</button>
            </div>
        );
    }
}


class TestInput extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            text: [],
        };
    }
    componentWillMount() {
        this.setState({ text: this.props.text });
    }
    componentWillReceiveProps(nextProps) {
        this.setState({ text: nextProps.text });
    }

    btnClick() {
        const text = this.state.text;
        text.push('click');
        this.setState({ text });
    }
    render() {
        return (
            <div>
                <button ref="btn" className="btn btn-primary" onClick={this.btnClick.bind(this)}>Click</button>
            </div>
        );
    }
}
TestInput.propTypes = {
    text: PropTypes.array,
};

When I click the "Click" button in TestInput Component, it push a "click" string to its state. Then, I click "show click result" button in Message page, it console the state as ["click"]. But I did not use setState method in Message. I use React with version 15.4.2. PS: Maybe there was an issue like this, could someone @ me and I'd close mine.

ip192 avatar May 10 '17 09:05 ip192

I’m not sure what you’re trying to do, but I think you might be a bit confused about how state works.

You don’t need to “synchronize” two different component states. Instead, let the parent component own the state, and pass it down as props to the child. Then they’ll stay in sync.

I suggest revisiting Lifting State Up or a similar section in the React tutorial.

Since this doesn’t look like a bug, I’ll close the issue, but I hope these links help.

gaearon avatar May 10 '17 09:05 gaearon

In your particular example, you see ["click"] in the parent component because you mutated the array passed from your parent component. This is in part why we don’t recommend mutation.

I very much suggest that you go through our tutorial fully because it explains these exact issues.

gaearon avatar May 10 '17 09:05 gaearon

I think the reason why this happened is that I changed the same state object no matter it's in sub or parent component. And thank you for your advice, I'd go on reading the tutorial.

ip192 avatar May 10 '17 10:05 ip192

Yes, this is correct.

gaearon avatar May 10 '17 13:05 gaearon