`typeof React.forwardRef(...)` is not a 'function', breaking third-party libs
See original GitHub issueDo you want to request a feature or report a bug?
Bug, possibly
What is the current behavior?
The new React.forwardRef
feature returns an object, not a function.
To this point, the typeof
both stateless components and class components was 'function'
.
React does not expose a utility to check whether a given value is valid to be passed to React.createElement
. As a result, third-party library authors who wish to pre-validate a component type will commonly use typeof Component === 'function'
to test whether the type is valid. The return value of forwardRef
, despite being a valid component type, will fail this naive test. Library authors do this in order to provide better errors than would otherwise be provided by React if an invalid component type was passed in.
In a perfect world, the fault would fall squarely on the third-party library developers for using an imprecise check, but those developers could rightfully point out that React has given them little ability to be precise in these pre-emptive checks.
~This is one of those “don’t break the internet” bugs.~ This is a bug where third-party developers have taken reasonable-yet-illegal dependencies on React internals in order to provide a better experience for their users. If it’s not too difficult, it may be a good idea to chane forwardRef
to return a 'function'
, instead of an object.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.3.0-alpha.3
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:26 (20 by maintainers)
Sure, but you can make the same argument about
my-component-library
starting to depend on a new feature that was introduced in a minor version of React, results in an observably different behavior, and didn’t exist before. Which is exactly my point: relying on a new feature is a breaking change whereas adding a new feature is not.I don’t understand what you’re suggesting.
forwardRef()
doesn’t accept a component at all (it accepts a rendering function). There are good reasons why the returned type is not a function (see last paragraph of https://github.com/reduxjs/react-redux/issues/914#issuecomment-396410016). We don’t plan to cut a patch to change this.Again, a library adopting
forwardRef
should constitute a major bump for that library. Even if React did something magical to ensureforwardRef
return value is a function it still has a different observable behavior (namely, it changes what a ref points to, and depends on a method that does not exist in older versions), so a library would have to bump its major version anyway.This new method has been added to
react-is
and will go out with the upcoming release. Thanks!