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.

useRef does not persist value when used with createRoot

See original GitHub issue

Do you want to request a feature or report a bug?

Report a bug.

What is the current behavior?

React.useRef does not persist value when using with ReactDOM.createRoot. It renders child component of main app component twice and does not persist ref value between these renders.

When we use React.useRef with ReactDOM.render it renders child component of main app component only once.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

https://codepen.io/sudesh955/pen/zYYzdrg?editors=1011

What is the expected behavior?

It should persist ref across renders. For more details see comments in codepen link.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

version 0.0.0-experimental-f6b8d31a7 on chrome Version 78.0.3904.70 (Official Build) (64-bit) on ubuntu.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Oct 15, 2020

I’m not sure I’m following why your conclusion is that this is a bug in React.

Can you explain the behavior you’re expecting? Conceptually, StrictMode intentionally completely “throws away” everything from one of the render attempts. That does include refs, by design. Rendering should not be making side effects, so throwing away the ref values during one of the renders should be completely safe. This would only affect the logic if you’re doing something else — for example, subscribing to a store — which you should not do while rendering because rendering should not have side effects. This is exactly what StrictMode is supposed to catch.

Does this explanation help?

3reactions
lostpebblecommented, Sep 21, 2020

@probablyup yep, this is causing a myriad of issues in my library now as well. It seems that the state in useRef is not being persisted properly between renders and its causing component cleanup and no-op issues because of that (certain flags set in useRef’s state are not being maintained).

People have opened up 2 issues related to this in the past couple days. It took me a while to figure out that CRA uses React.StrictMode by default, so many people are now running into this issue. Would appreciate some feedback about this and how it can be resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRef value not persisting - Stack Overflow
React does not 'react' to ref values changes so your child component will not be rerendered if the ref value changes. Use a...
Read more >
useRef - React Docs
useRef is a React Hook that lets you reference a value that's not needed for rendering. const ref = useRef(initialValue).
Read more >
React useRef Hook - W3Schools
The useRef Hook allows you to persist values between renders. It can be used to store a mutable value that does not cause...
Read more >
Build a React Timer Application with useRef
A quick fix is to use Refs. Ref is similar to state in that it persists between renders of your component, but it...
Read more >
Using Autocomplete with React - Algolia
It uses the useRef and useEffect hooks to create and mount the component. It doesn't define specific sources. Rather, you can pass sources ......
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