K
K
Korifa2019-07-25 12:49:29
React
Korifa, 2019-07-25 12:49:29

Why doesn't the id field change in state react?

When you click on the link, the address in the url line changes, but in the console I see that the id of the class does not change5d397b249ce8e041534361.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2019-07-25
@Korifa

no need to write props to the state, it's better to work immediately with this this.props.....number where
this Worker component is needed each time a new one is created, or is it created only once, and then after updating the url, you need to change the content in it? the constructor is executed only 1 time, that is, this.state.id is set once from the props, and then not anymore,
or so
https://ru.reactjs.org/docs/react-component.html#c...
---
ibid
https://ru.reactjs.org/docs/react-component.html#c...

Note:
Don't copy props into the state! This is a common mistake
...

M
Mikhail Osher, 2019-07-25
@miraage

https://overreacted.io/writing-resilient-components/
Read the article very carefully and find the cause of the problem.
// edit
The id parameter in state is written only in the constructor. For it to be updated, either getDerivedStateFromProps should be used, or setState should be called in componentDidUpdate if "props.params.match.number" has changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question