Skip to content

Instantly share code, notes, and snippets.

@shirbr510
Created September 9, 2020 09:27
Show Gist options
  • Save shirbr510/47ddaca990dffc4527d629261d8e3c65 to your computer and use it in GitHub Desktop.
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)
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