-
Notifications
You must be signed in to change notification settings - Fork 45.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
forwardRef precludes use of composite component test utils methods #13455
Comments
I think this works as expected. The I understand how this can be annoying. The truth is that |
Thanks, @gaearon, I was afraid of that. To be honest, I find |
Have you considered keeping the original type attached as a property to the wrapped one? Then you can search for it in tests. I don’t see the difficulty. |
No, I didn't think of that, and that might work, but I'm not sure I completely understand what you mean. I try not to dive into React internals in our application code, but are you suggesting something like attaching |
I'm thinking something like: const withMyContext = (Component) => {
class MyContextConsumer extends React.Component {
render() {
// ... same
}
}
return {
...React.forwardRef((props, ref) => (
<MyContextConsumer {...props} forwardedRef={ref} />
)),
type: Component
};
}; and then I can call things like: findRenderedComponentWithType(tree, myHOCForwardedComponent.type) Is that what you were referring to? Thanks again for the time. |
I’m not sure spreading a function like this would work. I just mean const Result = forwardRef(...)
Result.Original = Component
return Result And then yes, search for the |
Gotcha. I spread the function return, which should be an object; seems to work fine. Thanks for the tip!! |
Ahh right it's an object. Yeah. |
@gaearon the solution worked beautifully in development. Oddly, I deployed to production and in the production build only the I've been debugging for a bit and haven't come up with anything concrete. I'll try to put together a fiddle in the next couple days. But it was really concerning to me that it worked so well in dev, and it was only in prod that I ran into issues. Have you heard about anything like this before with One last piece of info is that I kept my backup branch of not using |
Do you mean you used TestUtils in production? We’ll need a reproducing case if something didn’t work. |
No, sorry, this has nothing to do with TestUtils, this has to do with the React.Context HOC that returns a forwardRef as written in the first comment. I will try to repro, but I'm worried that it will be difficult because it's flakey—I have a few instances of about 100 in my app where it didn't work, and each one that didn't work is very different. So I'll need to spend more time on it to figure out how to replicate. Just wanted to raise it in case you'd seen/heard anything about it before and had any insight. It only happens in the production build. |
Not aware of any issues in the latest version. |
Digging in a little more, I believe the issue is from uglify, maybe at the compression step (when I use |
Unfortunately Uglify is notably buggy with ES6 input. I'd suggest always transpiling to ES5 before Uglifying. |
The ES6 branch is no longer maintained; either switch to terser (fork of the ES6 branch of uglify) or always use uglify in |
if it's no longer maintained, why is webpack still using it? |
The ES5 branch of uglify is still maintained. Webpack still uses it by default because of semver concerns; only webpack@5 can change what minifier is used by default. I don't know if terser will be used, but there's at least a will to decouple uglifyjs-webpack-plugin as a direct dependency of webpack core. There is also https://github.com/webpack-contrib/terser-webpack-plugin |
i don't actually think that's true: https://webpack.js.org/plugins/uglifyjs-webpack-plugin/, and i know that from our experience, we are shipping minified ES6 code and up until now we haven't changed the default minifier settings—i imagine that using the ES5 branch to minify ES6 code wouldn't work. |
Yes, because it'll still work if you happen to not be hitting the bugs that the uglify ES6 branch had. |
Fix tests broken by using ReactTestUtils on forwardRef components. (See facebook/react#13455)
Do you want to request a feature or report a bug?
Bug, I believe—requested to file a new issue per #12453 (comment)
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
: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:
What is the expected behavior?
I would hope that we could still navigate the tree, such that
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!!
The text was updated successfully, but these errors were encountered: