You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure. The core of the issue is React.forwardRef passing null as ref argument to the wrapped component if the parent component doesn't pass a ref. undefined would be better in this case, but there might be a reason to pass null that I'm not aware of, so I'm not comfortable calling this a bug.
MyComponent will receive null as ref argument when a parent component doesn't pass a ref. Leaving aside that undefined better represent the fact that something was not passed than null, this also makes it impossible to use the forwarded ref internally in MyComponent because it will be null and we cannot reassign the prop.
I've been able to work around this using the following solution (excuse my classyness please, shouldn't matter much for the example):
classMyComponentextendsReact.Component{el=React.createRef();componentDidMount(){if(!this.el.current){this.el=this.props.innerRef;}if(!this.el.current)return;// bind event handlers to this.el}render(){constchildRef=this.props.innerRef||this.el;return<buttonref={childRef}>Look at my button</button>;}}
While the above works, it's not the nicest piece of code and there's code in two places in order to support adapting to the case when parent doesn't pass a ref.
What is the expected behavior?
Not only is undefined a less surprising value for ref argument when no ref is passed by the parent, but I think it would also allow us to use defaultProps to make a new ref the default when nothing is passed in. My initial attempt was something like:
classMyComponentextendsReact.Component{staticdefaultProps={innerRef: React.createRef(),};componentDidMount(){if(!this.innerRef.current)return;// bind event handlers to this.innerRef.current}render(){return<buttonref={innerRef}>Look at my button</button>;}}
but it didn't work because innerRef is null.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.9.0
Chrome 76.0.3809.132 on Linux
Didn't try with older versions of React or in other browsers.
The text was updated successfully, but these errors were encountered:
In this case, you would probably not want to use a defaultProp at all. If you render multiple <MyComponent /> they will all read and write the same innerRef.
Each <MyComponent /> will need to create its own ref as well as handle props.innerRef. You might want to take a look at the discussion in #13029, where people have examples of multiple refs.
Do you want to request a feature or report a bug?
Not sure. The core of the issue is
React.forwardRef
passingnull
asref
argument to the wrapped component if the parent component doesn't pass a ref.undefined
would be better in this case, but there might be a reason to passnull
that I'm not aware of, so I'm not comfortable calling this a bug.What is the current behavior?
Given:
MyComponent
will receivenull
asref
argument when a parent component doesn't pass a ref. Leaving aside thatundefined
better represent the fact that something was not passed thannull
, this also makes it impossible to use the forwarded ref internally inMyComponent
because it will benull
and we cannot reassign the prop.I've been able to work around this using the following solution (excuse my classyness please, shouldn't matter much for the example):
While the above works, it's not the nicest piece of code and there's code in two places in order to support adapting to the case when parent doesn't pass a ref.
What is the expected behavior?
Not only is
undefined
a less surprising value forref
argument when no ref is passed by the parent, but I think it would also allow us to usedefaultProps
to make a new ref the default when nothing is passed in. My initial attempt was something like:but it didn't work because
innerRef
isnull
.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.9.0
Chrome 76.0.3809.132 on Linux
Didn't try with older versions of React or in other browsers.
The text was updated successfully, but these errors were encountered: