Skip to content
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

Fix hoisting of react statics between two forwardRef components #71

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
# unreleased
- Fixed `defaultProps`, `displayName` and `propTypes` being hoisted from `React.forwardRef` to `React.forwardRef`. ([#71])

# 3.2.0 (November 26, 2018)
- Added support for `getDerivedStateFromError`. ([#68])
- Added support for React versions less than 0.14. ([#69])
Expand All @@ -19,5 +22,6 @@
[#62]: https://github.com/mridgway/hoist-non-react-statics/pull/62
[#68]: https://github.com/mridgway/hoist-non-react-statics/pull/68
[#69]: https://github.com/mridgway/hoist-non-react-statics/pull/69
[#71]: https://github.com/mridgway/hoist-non-react-statics/pull/71
[e0846fe]: https://github.com/mridgway/hoist-non-react-statics/commit/e0846feefbad8b34d300de9966ffd607aacb81a3
[e89c7a6]: https://github.com/mridgway/hoist-non-react-statics/commit/e89c7a6168edc19eeadb2d149e600b888e8b0446
5 changes: 4 additions & 1 deletion src/index.js
Expand Up @@ -29,7 +29,10 @@ const KNOWN_STATICS = {

const FORWARD_REF_STATICS = {
'$$typeof': true,
render: true
render: true,
defaultProps: true,
displayName: true,
propTypes: true
};

const TYPE_STATICS = {};
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/index.js
Expand Up @@ -226,4 +226,32 @@ describe('hoist-non-react-statics', function () {
expect(WrappedFancyButton.render).to.not.equal('bar');
});

it('should not mix defaultProps, displayName and propTypes in forwardRef', () => {
const Component = React.forwardRef((props, ref) => null);
Component.defaultProps = {
message: 'forwarded'
}
Component.displayName = 'BaseComponent';
Component.propTypes = {
id: () => new Error()
}
Component.foo = 'foo';

const EnhancedComponent = React.forwardRef(({id, ...props}, ref) => <Component {...props} ref={ref} />);
EnhancedComponent.defaultProps = {
id: 'stop-me'
}
EnhancedComponent.displayName = `Enhanced(${Component.displayName})`;
EnhancedComponent.propTypes = {
innerRef: () => 'deprecated'
}

hoistNonReactStatics(EnhancedComponent, Component);

expect(EnhancedComponent.foo).to.equal('foo');
expect(EnhancedComponent.displayName).to.equal('Enhanced(BaseComponent)');
expect(EnhancedComponent.defaultProps.id).to.equal('stop-me');
expect(EnhancedComponent.propTypes.innerRef()).to.equal('deprecated');
})

});