Note the hidden danger of bind (this) When react implements pure render !, Reactrender
I will not talk much about pure render. I will attach another article link to my article on how to maximize react performance (Prepass)
No matter you do not need immutable, as long as you want to reach the pure render, the following is worth your attention!
One day, as usual, I was happy to write react, using @ pureRender,
Export default class extends Component {... render () {const {name, age} = this. state; return (<div> <Person name = {name} age = {age} onClick = {this. _ handleClick. bind (this) }></Person> // Where the bug is located </div> )}...}
A problem is found. For the child component "Person", when the parent component "re-render" does not change the two props before and after "Person", it will still re-render, that is, immutable. js is not easy to use.
Originally, every time the parent component render and _ handleClick execute bind (this), the reference of _ handleClick will be changed every time, so the two props before and after Person are actually different.
What should we do? Remove bind (this?No, it must be used.
The real answer is that the parent component does not execute bind (this) every time the render is render. It is executed directly in the constructor in advance and after modification
Export default class extends Component {constructor (props) {super (props) this. _ handleClick = this. _ handleClick. bind (this) // change to this} render () {const {name, age} = this. state; return (<div> <Person name = {name} age = {age} onClick = {this. _ handleClick }></Person> </div> )}...}
Reference: React. js pure render performance anti-pattern
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.