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.

Callback ref is passed null and then the component again

See original GitHub issue

I have a super simple React (Native) component with a static root with a callback ref. When the component is mounted, the callback ref is invoked with the root component. Then when I call setState the callback ref receives null and then the root component again.

class Example extends React.Component {
  constructor(props, context) {
    super(props, context);
    console.log('in the constructor');
  }

  render() {
    return (
      <View
        key="root"
        ref={component => { console.log('got ref', component); }}
        style={this.props.style}
      />
    );
  }

  componentDidMount() {
    console.log('in componentDidMount');
    this.setState({});
  }
}

The console logs read:

in the constructor
got ref R…s.c…s.Constructor {props: Object, context: Object, refs: Object, updater: Object, state: null…}
in componentDidMount
got ref null
got ref R…s.c…s.Constructor {props: Object, context: Object, refs: Object, updater: Object, state: null…}

The null ref is confusing to me since the ref’d view isn’t unmounted nor is its parent. I believe this is under 0.14 beta 1 (whatever RN master uses).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:28 (13 by maintainers)

github_iconTop GitHub Comments

8reactions
unelcommented, Nov 17, 2016

@spicyj

The function instance is different so we pass null to the old one and the component to the new one.

But… why?.. And why is this not reflected in the documentation? 😢

6reactions
gaearoncommented, Nov 17, 2016

@unel

But… why?

Because the function instance is different on every render. React doesn’t know it’s the same function “conceptually”. Maybe you were passing callback1 and now pass callback2. So it needs to reset the ref for callback1 (since it might never get called again) and then set the ref for callback2 (since it’s the new one).

This could be added to the documentation but I’m not sure why it matters. Can you help me understand? If you just set a field in the ref callback you shouldn’t have to think about this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When the ref callback argument is NULL in react?
React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts.
Read more >
Avoiding useEffect with callback refs - TkDodo's blog
This function gets the rendered DOM node passed as argument. If the React element unmounts, it will be called once more with null....
Read more >
Refs and the DOM - React
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 >
React Callback Refs — a Complex Case | by E.Y. - Medium
If the ref callback is defined as an inline function, it will get called twice during updates, first with null and then again...
Read more >
Using refs in React functional components (part 1) - useRef + ...
If the ref callback is defined as an inline function, it will get called twice during updates, first with null and then again...
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