Redux: How do partial re-renderings work?

妖精的绣舞 提交于 2020-04-30 06:24:40

问题


This question is about internals for partial re-renderings with React-Redux.

To explain what I mean, I will first introduce a very crude technique for managing state without any state management libary. The technique uses a a huge "AppState"-object that is owned by the top-level App-component. Suppose that this AppState holds not only state-properties, but also several callbacks that mutate those state-properties. Furthermore, suppose that we use props to pass down this AppState throughout the entire component hierarchy. Thanks to the ES6-spread syntax, passing a huge number of props can be done without a lot of boilerplate code. In the top-level App-component, it will look like this:

<ChildComponent {...this.state} />

In all other components, it will look like this:

<GrandChildComponent {...this.props} />

It is important to note that the ES6-spread syntax does not actually pass the AppState-object. Instead, it extracts all the AppState-properties and passes them as separate props.

Now we should distinguish between top-level properties and nested child-properties of the AppState:

  • If I mutate a top-level property of this AppState by calling setState, then the entire app will re-render (unless I use things like pure components).
  • However, if I change a nested child-property of this AppState, then nothing will happen because React does not notice the property change.

This leads to my final questions:

  • What is the render-performance of this crude approach in comparison to Redux?
  • How exactly does Redux handle "partial renderings", such that only some of the Components re-render after a state mutation?

回答1:


If I mutate a top-level property of this AppState by calling setState, then the entire app will re-render (because everything depends on the AppState).

If you mutate and use pure components then nothing will render, you change state by creating a new state object.

However, if I mutate a nested child-property of this AppState, then nothing will happen because React does not notice the property change.

This is only true if you mutate and components are pure.

What is the render-performance of this crude approach in comparison to Redux?

Prop drilling will re render the entire tree but branches that use state that didn't change won't re render if they are pure. Prop drilling is bad for maintenance because if you need to refactor grand child state logic you may need to refactor the whole tree or branch. But from a performance point it would not take a big hit provided that you use pure components and are careful when passing callbacks and not re creating them on every render (see useCallback).

How exactly does Redux handle "partial renderings", such that only some of the Components re-render after a state mutation?

React-redux useSelector or connect mapStateToProps are always called every time dispatch changed state and before rendering.

If the result is different than last result then react-redux will trigger render of the component. If the component gets props then a render could also be triggered because props change and mapstate/selector will be executed.

A connected component will observe state and render when the result of mapState or selector has changed. An example app with logs showing what react-redux will execute can be found here




回答2:


For state management, you don't necessarily have to use Redux, if your use cases are small, maybe React Hook would be perfect for you.

For React rerendering matter, what I know is there are several strategies (useMemo, PureComponents) provided by React for managing and improve the performance. It really depends on how you manage your components.

One example is using PureComponent, even if you have a large state in your top-level app.js, if you manage the child components properly, they will not re-render if their receiving props haven't changed.



来源:https://stackoverflow.com/questions/61133292/redux-how-do-partial-re-renderings-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!