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.

Component gets unmounted = ref is null while I interact with a component

See original GitHub issue

Hello Michele!

I was playing with this example in Styleguidist cookbook: how-to-use-refs-in-examples

Took me quite a while to understand why the example would not work on my almost clean version of styleguidist + snapguidist.

When I click the button in the even more simplified example below I get an error because snapguidist basically unmount the component so ref gets called with null (is that correct?)https://github.com/facebook/react/issues/9328#issuecomment-298438237

Could this lead to issues? I’m interacting with a component that is unmounted?

Example to try on a clean installation of styleguidist+snapguidist

let myEl;
<div>
  <button onClick={() => myEl.value = 'Pizza'}>Insert</button>
  <input ref={ref => myEl = ref} />
</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
MicheleBertolicommented, Feb 4, 2018

This package has been updated to the latest version of Styleguidist (6.2.2) and React (16.2.0). The issue is not solved yet (unfortunately), but there’s a workaround:

<input ref={ref => ref && (myEl = ref)} />
0reactions
MicheleBertolicommented, Aug 10, 2018

Hello @goransm, this seems a different problem. Would you mind opening a new issue and provide a non-working example? Thank you very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: ref to child component is null after remounting by ...
I know that refs are set to null when a component unmounts, but by rendering a new version of Game , I would...
Read more >
Refs and the DOM
React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts. Refs...
Read more >
Avoiding useEffect with callback refs - TkDodo's blog
Interacting with DOM nodes doesn't necessarily need useEffect. ... It will be set back to null when the component is unmounted.
Read more >
React: Prevent state updates on unmounted components
In the above case, React tries to set the state of an unmounted component, which is not necessary since the component is not...
Read more >
Refs to Components | React
Note that when the referenced component is unmounted and whenever the ref changes, the old ref will be called with null as an...
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