forwardRef precludes use of composite component test utils methods
See original GitHub issueDo 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:
- Created 5 years ago
- Comments:19 (10 by maintainers)

Top Related StackOverflow Question
Unfortunately Uglify is notably buggy with ES6 input. I’d suggest always transpiling to ES5 before Uglifying.
Digging in a little more, I believe the issue is from uglify, maybe at the compression step (when I use
minimize: falsein 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.