Created
September 9, 2020 09:27
-
-
Save shirbr510/47ddaca990dffc4527d629261d8e3c65 to your computer and use it in GitHub Desktop.
A HOC that allows generic props mutations (just like ReactRedux.connect, but without being connected to anything)
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" | |
/** | |
* A HOC that allows generic props mutations (just like ReactRedux.connect, but without being connected to anything) | |
* @param WrappedComponent A React component to wrap | |
* @param mapPropsToProps a logic that computes new props out of existing props | |
* @returns A wrapped Component that has new computed props OOTB | |
*/ | |
const withComputedProps = (WrappedComponent, mapPropsToProps) => props => { | |
let computedProps = {}; | |
if (mapPropsToProps) { | |
computedProps = mapPropsToProps(props); | |
} | |
return <WrappedComponent {...props} {...computedProps} />; | |
}; | |
export default withComputedProps; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment