-
Notifications
You must be signed in to change notification settings - Fork 93
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
React.forwardRef results in propTypes etc. being hoisted #66
Comments
I think this is only currently a problem for hoisting from ForwardRef to ForwardRef since we also check against the target component's list of statics as well. In your example, the only warning I'm seeing is that I think this is solvable for ForwardRef to ForwardRef hoisting (and future types if there are more). |
Right. I constructed this from the code but thought that my mistake is yours. I though this was also an issue for I update the sandbox to illustrate the actual issue you described: import React from "react";
import ReactDOM from "react-dom";
import hoistStatics from "hoist-non-react-statics";
import PropTypes from 'prop-types';
const App = React.forwardRef((props, ref) => <div {...props} ref={ref} />)
App.propTypes = {
id: PropTypes.number.isRequired
}
App.defaultProps = {
children: 'Hello, World',
}
const withDefaultId = id => Component => {
const EnhancedComponent = React.forwardRef(function Enhanced(props, ref) {
return <Component id={id} {...props} ref={ref} />
})
return hoistStatics(EnhancedComponent, Component);
}
// we should now be able to savely use AppWithId without propTypes warning
// that `id` is missing since with injected it. But propTypes is being hoisted
// so react warns anyway
const AppWithId = withDefaultId(1)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<React.StrictMode><AppWithId ref={r => console.log(r)} /></React.StrictMode>, rootElement); |
I have just been hit by the same issue with // Component is using forwardRef
function WithStyles(props) {
return <Component {...props} />
}
WithStyles.displayName = 'Foo';
hoistNonReactStatics(WithStyles, Component);
// WithStyles.displayName === Component.displayName, it should be Foo |
@oliviertassinari Looks like you're using an outdated version. Latest only has an issue if both components are const Component = React.forwardRef((props, ref) => null)
Component.displayName = 'Bar';
function WithStyles(props) {
return <Component {...props} />
}
WithStyles.displayName = 'Foo';
hoistStatics(WithStyles, Component);
// passes
console.assert(WithStyles.displayName === 'Foo');
const BadStyles = React.forwardRef((props, ref) => null);
BadStyles.displayName = 'Foo';
hoistStatics(BadStyles, Component);
// only this fails
console.assert(BadStyles.displayName === 'Foo'); |
@eps1lon Yes, both components are using |
Working on a fix. |
It's worse than what I thought. On a Node.js environment, it overrides the render method 😱. |
Sounds like a separate issue. It should already prevent hoisting of |
propTypes
anddefaultProps
are still processed statics for react inforwardRef
components (other statics might be too but those two were the most straight forward to verify). They should therefore not be hoisted.Edit:
displayName might be another issue. It's probably safe to assume that
FORWARD_RED_STATICS
should be merged if the sourceComponent is a forwardRef component instead of replacing all react statics.full sandbox
Edit: broken example; see #66 (comment) for fixed code
The text was updated successfully, but these errors were encountered: