Comments (8)
@oliviertassinari Looks like you're using an outdated version. Latest only has an issue if both components are forwardRef
:
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');
from hoist-non-react-statics.
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 id
is an invalid type (string instead of number).
I think this is solvable for ForwardRef to ForwardRef hoisting (and future types if there are more).
from hoist-non-react-statics.
Right. I constructed this from the code but thought that my mistake is yours. I though this was also an issue for forwardRef
-> class component
but was unable to reproduce.
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);
from hoist-non-react-statics.
I have just been hit by the same issue with displayName
. Here is a pseudo reproduction:
// Component is using forwardRef
function WithStyles(props) {
return <Component {...props} />
}
WithStyles.displayName = 'Foo';
hoistNonReactStatics(WithStyles, Component);
// WithStyles.displayName === Component.displayName, it should be Foo
from hoist-non-react-statics.
@eps1lon Yes, both components are using forwardRef
, like in your example. I should have taken more time and provide an actual reproduction.
from hoist-non-react-statics.
Working on a fix.
from hoist-non-react-statics.
It's worse than what I thought. On a Node.js environment, it overrides the render method 😱.
from hoist-non-react-statics.
Sounds like a separate issue. It should already prevent hoisting of render
. There's a test for that already. Better open a new issue for that.
from hoist-non-react-statics.
Related Issues (20)
- Minified build in npm package makes auditing difficult
- how can I use this package to extract atrributes of dynamic import react component
- Allow react version ^17 HOT 3
- `isMemo` check always returns false for components HOT 2
- Please update description in understendable way
- tsc build declaration error: Subsequent property declarations must have the same type.
- error: Error: While trying to resolve module `hoist-non-react-statics`
- Please release a new version which uses inclusive language
- Drop React <16.0.0 support? HOT 5
- missing dist/ folder in 3.3.0 release on GitHub HOT 3
- Use the latest react-is feature "isMemo" but without upgrading to the correct version of react-is HOT 10
- Why not supported copy static defaultProps, displayName? HOT 4
- When hoisting: Keep prop value if context value is undefined HOT 7
- Object.getPrototypeOf called on non-object HOT 1
- Reduce bundle size HOT 4
- Incorrect AMD definition HOT 2
- Remove publishConfig block from package.json HOT 3
- Please tell Version for react-redux HOT 1
- Improve typing of third argument HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hoist-non-react-statics.