Created
March 9, 2020 12:19
-
-
Save desphilboy/3c6e2c8169227e22b2a37d18b201534f to your computer and use it in GitHub Desktop.
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"; | |
import { getComponentName } from "../utils"; | |
/* adds some more props on the component to perserve component purity and independence. | |
* can be called on a component only once ( not cascadable ) | |
* Params: | |
* @extraProps is an mapping object or function retruning a mapping object which contains new params. | |
* if is a funcion, will be called with existing props. | |
* @BaseComponent is the component to be decorated with more props | |
* @props are the existing props passed by parent | |
*/ | |
export function withProps(extraProps) { | |
return BaseComponent => | |
// eslint-disable-next-line react/prefer-stateless-function | |
class WithProps extends React.Component { | |
static displayName = `withProps(${getComponentName( | |
BaseComponent | |
)})`; | |
render() { | |
return ( | |
<BaseComponent | |
{...this.props} | |
{...(typeof extraProps === "function" | |
? extraProps(this.props) | |
: extraProps)} | |
/> | |
); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment