Created
August 14, 2019 08:29
-
-
Save Natumsol/eb8d62b308174da0b27470033f669a27 to your computer and use it in GitHub Desktop.
withProps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| // use forwardRef polyfills, preparing for future upgrades to React 16 | |
| import forwardRef from 'create-react-ref/lib/forwardRef'; | |
| const withProps = (defaultProps = {}) => Base => { | |
| class WithRef extends React.Component { | |
| render() { | |
| const { forwardRef, ...rest } = this.props; | |
| const mergedProps = { ...defaultProps, ...rest }; | |
| return ( | |
| <Base ref={forwardRef} {...mergedProps} /> | |
| ); | |
| } | |
| } | |
| return forwardRef((props, ref) => <WithRef {...props} forwardRef={ref} />); | |
| }; | |
| export default withProps; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment