What is hoist-non-react-statics?
The hoist-non-react-statics package is used to copy non-react specific statics from a child component to a parent component. This is often used in higher-order component (HOC) patterns, where the HOC wraps a child component but needs to expose the same static methods as the child.
What are hoist-non-react-statics's main functionalities?
Hoisting non-react statics
This feature allows you to automatically copy static methods from a child component to a parent component, which is useful when creating higher-order components that should not interfere with the static methods of the wrapped component.
import hoistNonReactStatics from 'hoist-non-react-statics';
function enhance(WrappedComponent) {
class Enhance extends React.Component {
/* ... */
}
hoistNonReactStatics(Enhance, WrappedComponent);
return Enhance;
}
// Usage
// If WrappedComponent had static method foo(), Enhance will also have it.
Other packages similar to hoist-non-react-statics
babel-plugin-transform-react-remove-prop-types
While not directly related to hoisting statics, this plugin removes propTypes from React components in production builds, which can be a complementary step when optimizing higher-order components.
hoist-non-react-statics
Copies non-react specific statics from a child component to a parent component.
Similar to Object.assign
, but with React static keywords blacklisted from
being overridden.
$ npm install --save hoist-non-react-statics
Usage
import hoistNonReactStatics from 'hoist-non-react-statics';
hoistNonReactStatics(targetComponent, sourceComponent);
If you have specific statics that you don't want to be hoisted, you can also pass a third parameter to exclude them:
hoistNonReactStatics(targetComponent, sourceComponent, { myStatic: true, myOtherStatic: true });
What does this module do?
See this explanation from the React docs.
Compatible React Versions
Compatible React Version | hoist-non-react-statics Version |
---|
16.3+ With ForwardRef Support | >= 3.x |
16.3+ Without ForwardRef Support | >= 2.5.0 |
0.13-16.2 | >= 1.0.0 |
Browser Support
This package uses Object.defineProperty
which has a broken implementation in IE8. In order to use this package in IE8, you will need a polyfill that fixes this method.
License
This software is free to use under the Yahoo Inc. BSD license.
See the LICENSE file for license text and copyright information.
Third-party open source code used are listed in our package.json file.