R
R
Roman Sokolenko2020-05-16 17:27:46
React
Roman Sokolenko, 2020-05-16 17:27:46

Why does React change one state synchronously after another is changed?

The fact is that my react component unexpectedly changes one state property for me, although the handler changes another.
Conditionally:
1. The state still has properties
this.state = { content: {}, given_content: {} }
2. In componentDidMount , data from fetch() gets to both states:
componentDidMount() {
fetch(get_api_url()).then (response => {
return response.json();
}).then(data => {
let content = data.data;
...
this.setState({ content: content, given_content: content });
});
}
3. The markup of the component has a select element with an attached handleChange handler:
handleChange(event) {
this.setState(function (state, props) {
let data = state.given_content;
delete data['Euro'];
return({ given_content: data }); }
)
}
this.state.given_content BUT changes
this.state.content too!! Although setState is not called anywhere else, I don’t touch content at all anywhere else!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question