Callback ref is passed null and then the component again
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:9
- Comments:28 (13 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@spicyj
But… why?.. And why is this not reflected in the documentation? 😢
@unel
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 passcallback2
. So it needs to reset the ref forcallback1
(since it might never get called again) and then set the ref forcallback2
(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.