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.

`typeof React.forwardRef(...)` is not a 'function', breaking third-party libs

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:26 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
gaearoncommented, Jun 11, 2018

If my-component-library depends on a new major version of React, shouldn’t my-component-library receive a major version bump as well?

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 ensure forwardRef 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.

2reactions
bvaughncommented, Mar 29, 2018

This new method has been added to react-is and will go out with the upcoming release. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Component is not a function on adding forwardRef
is there anything I have to change to add a forwardRef to this component? javascript · reactjs · react-native · react-hooks · react-forwardref....
Read more >
Refs and the DOM - React
If you want to allow people to take a ref to your function component, you can use forwardRef (possibly in conjunction with useImperativeHandle...
Read more >
property current does not exist on type forwardref - You.com
Refs are not necessarily objects with a current property. They can also be functions. So the type error is pointing out that you...
Read more >
Releases - styled-components
Fix React Native components accepts function as style prop. ... (a trait shared by most CSS-in-JS libraries, but not the third party library...
Read more >
Refs and the DOM | React - ReScript
As the error message implies, If you want to allow people to take a ref to your component function, you can use ref...
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