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.

forwardRef precludes use of composite component test utils methods

See original GitHub issue

Do you want to request a feature or report a bug? Bug, I believe—requested to file a new issue per https://github.com/facebook/react/issues/12453#issuecomment-414868619

What is the current behavior? When using ReactTestUtils that navigate the trees for composite components, I am unable to find instances of components wrapped in React.forwardRef:

findRenderedComponentWithType(tree, myHOCForwardedComponent)
// error, finds 0 instances

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem:

JSFiddle link here

I have a HOC that returns a forwardRef pretty much exactly like the one written up in the docs, except while using React Context:

const MyContext = React.createContext(someDefault);

const withMyContext = (Component) => {
  class MyContextConsumer extends React.Component {
    render() {
      const {forwardedRef, ...rest} = this.props;

      return (
        <MyContext.Consumer>
          {(value) => (
            <Component
              {...rest}
              ref={forwardedRef}
              myValue={value}
            />
          )}
        </MyContext.Consumer>
      );
    }
  }

  return React.forwardRef((props, ref) => (
    <MyContextConsumer {...props} forwardedRef={ref} />
  ));
};

@withMyContext
class MyHOCForwardedComponent extends React.Component {
  render() {
     return <div>HELLO</div>;
  }
}

What is the expected behavior? I would hope that we could still navigate the tree, such that

findRenderedComponentWithType(tree, myHOCForwardedComponent)

is able to find the rendered instance.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 16.4—affected everywhere, I believe.

Thank you for the time!!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Aug 30, 2018

Unfortunately Uglify is notably buggy with ES6 input. I’d suggest always transpiling to ES5 before Uglifying.

1reaction
noahgrantcommented, Aug 30, 2018

Digging in a little more, I believe the issue is from uglify, maybe at the compression step (when I use minimize: false in my webpack setup, the prod build operates correctly). I have run into issues with that before. If I can pinpoint exactly what it is, I’ll make one more comment here just as a reference, but then I’ll file with them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ForwardRef component error: jest react native - Stack Overflow
Here is What I have done to resolve this problem: Just add this function : jest.mock("react", () => { const originReact = jest....
Read more >
React Top-Level API
React.forwardRef creates a React component that forwards the ref attribute it receives to another component below in the tree. This technique is not...
Read more >
Testing forwardRef and useImperativeHandle in React Hooks
The question is: How do you test show() using Jest and react-test-library ? Turns out, it's pretty straightforward. You can't use useRef() in...
Read more >
react-forwardref-utils - npm Package Health Analysis - Snyk
react-forwardref-utils. v1.1.0. Utils to help with React 16.3+ forwardRef method For more information about how to use this package see README.
Read more >
Using refs in React functional components (part 2)
Element for it. ForwardedInput uses the React.forwardRef to obtain the ref passed to it, so we can forward the ref down to the ......
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