最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React with Hooks: When do re-renders happen? - Stack Overflow

programmeradmin3浏览0评论

When will a React component using hooks re-render?

Let's assume the component:

  • Manages state with useState
  • Receives props from its parent

Will a re-render happen directly after the following events, and only at those points in time?

  • Component receives new props
  • state is updated

Related Question

Let's assume the component has several useState expressions and a user interaction causes multiple states to update.

Will the component re-render multiple times, once per state value that changed, or will it batch these related changes into one single re-render?

When will a React component using hooks re-render?

Let's assume the component:

  • Manages state with useState
  • Receives props from its parent

Will a re-render happen directly after the following events, and only at those points in time?

  • Component receives new props
  • state is updated

Related Question

Let's assume the component has several useState expressions and a user interaction causes multiple states to update.

Will the component re-render multiple times, once per state value that changed, or will it batch these related changes into one single re-render?

Share Improve this question asked Mar 11, 2019 at 17:06 MagnusMagnus 7,82114 gold badges64 silver badges101 bronze badges 5
  • react devtools is good to answer your question you just check the highlight option and see if your component is rending multiple times or not – Aziz.G Commented Mar 11, 2019 at 17:10
  • 1 State updates and prop updates will cause the component to re-render. Multiple synchronous state updates will be batched together into one re-render – Tholle Commented Mar 11, 2019 at 17:11
  • @Tholle i agree, hope that answer your question – Aziz.G Commented Mar 11, 2019 at 17:14
  • @G.aziz can you explain how to find it using react devtools please – CodingPanda Commented Jun 15, 2021 at 4:56
  • @BuddhikaW it's checkbox that you can find in options – Aziz.G Commented Jun 15, 2021 at 11:49
Add a comment  | 

2 Answers 2

Reset to default 15

A component will re-render in the following cases considering it doesn't implement shouldComponentUpdate for class component, or is using React.memo for function

  • Component receives new props
  • state is updated
  • Context value is updated(if the component listenes to context change using useContext)
  • Parent component re-renders due to any of the above reasons

Let's assume the component has several useState expressions and a user interaction causes multiple states to update.

Will the component re-render multiple times, once per state value that changed, or will it batch these related changes into one single re-render?

useState doesn't merge the updated values with previous ones but performs batching like setState and hence if multiple state updates are done together, one render take place.

Found this nice article on this topic. I'll add the summary in case someone needs a quick read.

What causes a render in react?

State modification

Component re-renders when its state is manipulated, simple as that. So, when you modify the state of your component it tends to re-renders with the new state value.

Passing Props

If the parent component has triggered a rerender all the child component will rerender too, generally you would only want the specific child components to rerender only if the component is consuming some props or the passed props are modified but that isn’t the case, it does not matter if the props are consumed, modified or not, the child components will re-render if the parent component has triggered a render.

Using the Context API

When modifying the state which is also the value passed to the context, the whole child components tree would get rerendered. Again doesn’t matter if the child component is consuming the context data or not, it will still rerender. The rerenders depend on the value passed to the provider but still, all the whole components tree would get rerendered.

Passing down references

In case of an object, array and, function comparison is based on references i.e. the exact memory location so, their comparison even though they seem equal yields false, in React Object.is method is used to compare the different values.

Example:

{}==={}             //false
[]===[]             //false
{a: 5}==={a: 5}     //false
[1,2,3]===[1,2,3]   //false

(when comparing the previous state to the new state, it returns false because they do not have the same reference. Only the value is the same)

发布评论

评论列表(0)

  1. 暂无评论