Understanding React?—?React 16.3 + Component life-cycle
Author Bartosz Szczeciński https://medium.com/@baphemot

Understanding React?—?React 16.3 + Component life-cycle

Hello, I'm Andrey Okhotnikov - CEO in React-Outsourcing. We are engaged in outsourcing web development , and in my spare time I help professional developers become even more professional.

The release of 16.3 introduced some new life-cycle functions, which replace existing ones to provide better support for the new asynchronous nature of React.

This article is an update on the previous one, which talked about the “old” lifecycle, thus it’s not going to focus on all of the lifecycles, only the new ones.

static getDerivedStateFromProps(nextProps, prevState)

The new function which main responsibility is ensuring that the state and props are in sync for when it is required. It’s main job is replacing componentWillReceiveProps

gDSFP is a static function and as such has no access to this — you are instead expected to return an object, which will be merged into the future state of the component (exactly like working with setState!)

The function is used when a component is updated but also when it is mounted, right after the constructor was called, so you no longer need to use constructor or class property form of state if you want to set initial state from props.

getSnapshotBeforeUpdate(prevProps, prevState)

Other of the two new functions, invoked in the so called “pre-commit phase”, right before the changes from VDOM are to be reflected in the DOM.

It is usable mostly if you need to read the current DOM state, for example you have an application in which new messages are added to the top of the screen — if a user scrolled down, and a new message is added the screen could move and make the UI harder to use. By adding getSnapshotBeforeUpdate you can calculate current scroll position and maintain it through the DOM update.

Even though the function is not static, it is recommended to return the value, not update the component. The returned value will be passed to componentDidUpdate as the 3rd parameter.

Deprecated functions

While the new functions are there to make your transition to AsyncMode easier, you are not forced to migrate all your code. The following functions will be marked as deprecated and in later releases renamed:

  • componentWillReceiveProps — UNSAFE_componentWillReceiveProps
  • componentWillUpdate — UNSAFE_componentWillUpdate

You will start seeing the deprecation warnings in next major version, and the function will be removed (the renamed versions will be kept!) in version 17.0

Dan Abramov summarized the whole change in one image for your pleasure:

Link to this article

If you need help on web development on the react - write to me, I will be glad to help

If you want to always be aware of what happens in React - subscribe to our page. 5 times a week I publish an article on the hottest topic.

Our team is always open to cooperation, we are ready to outsource the development of interesting products. Just write to me. Have a nice day

Site: https://react-outsourcing.com/

Email: [email protected]

要查看或添加评论,请登录

Andrey Okhotnikov的更多文章

  • An Introduction to Hooks in React

    An Introduction to Hooks in React

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • React-Redux connect(): when and how to use it

    React-Redux connect(): when and how to use it

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • React, Webpack and Babel from scratch

    React, Webpack and Babel from scratch

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • Good code practices with React and Redux

    Good code practices with React and Redux

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

    3 条评论
  • We’re under attack! 23+ Node.js security best practices

    We’re under attack! 23+ Node.js security best practices

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • React's Render Props Pattern - Children as a Function

    React's Render Props Pattern - Children as a Function

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • Redux vs. The React Context API

    Redux vs. The React Context API

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • I created the exact same app in React and Vue. Here are the differences.

    I created the exact same app in React and Vue. Here are the differences.

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

    3 条评论
  • Learn Render Props by Example

    Learn Render Props by Example

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

  • Styling React components

    Styling React components

    Hi, my name is Andrey Okhotnikov and I'm JavaScript developer. I develop single page applacations , and in my spare…

社区洞察

其他会员也浏览了