Note the hidden danger of bind (this) When react implements pure render !, Reactrender

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.