question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Functional component rerender when same state

See original GitHub issue

React version: 16.13.1

Steps To Reproduce

Link to code example

The current behavior

  1. first render -> ‘render’ log
  2. textA default value is ‘A’
  3. changeA Click and textA changed ‘AA’ -> render log
  4. changeA Click, before textA value is ‘AA’ after textA value is ‘AA’. -> render
  5. And then changeA Click, nothing ‘render’ log
  6. changeTextB Button Click and textB is always render. -> render
  7. changeTextA Button Click render log after changeTextB clicked

Jul-10-2020 10-31-25

The expected behavior

Expected behavior is do not render in Step4 and Step7. Because, before textA state is ‘AA’ and after textA state is ‘AA’. -> nothing change

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vkurchatkincommented, Jul 11, 2020

@heecheolman once again, quote from the same page

Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree.

Which means that when you call setState with the same value it is not guaranteed that the function is not going to be called again, but even if it is, it is not an actual rerender, since child components are not going to be rerendered.

0reactions
heecheolmancommented, Jul 11, 2020

example: codesandbox

  1. First Render: “render” print
  2. Click Set 1 Button: “render” print, Because state changes 0 to 1
  3. Click Set 1 Button: “render” print

In React document

If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.

If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)

Object.is(1, 1) returns true. But why React will render in Step3 ?

Similar stackoverflow question link

Read more comments on GitHub >

github_iconTop Results From Across the Web

react functional component re-render one time even the state's ...
1 Answer 1 · render useAutoIncrement. this is the first console message your component runs into when the function fires · CustomHook render....
Read more >
How and when to force a React component to re-render
Generally, forcing a React component re-render isn't best ... to see if both the preview and new value for state reference the same...
Read more >
Bug: setState does not rerender a functional component when ...
If I change the property of the same object, say changing the age field to 23 the rerender triggers fine. And why should...
Read more >
5 Ways to Avoid React Component Re-Renderings
useState() Hook is widely used in React applications to re-render the components on state changes. However, there are scenarios where we need to...
Read more >
When does React re-render components? - Felix Gerschau
As we already saw before, React re-renders a component when you call the setState function to change the state (or the provided function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found