propmatch is a useful utility for all high order components. It's useful because strings are brittle identifiers in composition, and it allows you to see where props are coming from in the wrapped component.
Install
react-propmatch is available on npm.
npm install react-propmatch
Guide
Note: This guide assumes you're using es6 with babel, and the optional 'es7.classProperties' transform.
Let's start by writing a simple high order component that sends the current time as a prop. The details are omitted for brevity.
{ return Component { ... } { ... } { ... } { return <Component ...thisprops time=thisstatetime /> } ;} @providesTime { return <div>thispropstime</div>; }
Because string keys are brittle and conflict prone, and the origin of props is often unclear, we can use react-propmatch to enable a clearer way of describing the relationship.
; var propTypes makeFactory = ; { var makeProps restPropTypes = ; return Component static propTypes = restPropTypes; { ... } { ... } { ... } { return <Component ...thisprops ... /> } ;} // expose the propTypesprovidesTimepropTypes = propTypes; @providesTime { return <div>thispropstime</div>; }
And this behaves exactly like the first example. So let's say that our clock want's to take a prop called 'time' which is a boolean. If true, don't render the date.
Damn, we have a conflict. Well instead of changing clock's api, or having to change the possibly third-party providesTime
, we just declare our propTypes.
@providesTime static propTypes = time: PropTypesboolean milliseconds: providesTimepropTypestime ; { var d = thispropsmilliseconds; return <div>thispropstime ? d : d</div>; }
react-propmatch looks at the propTypes and tries to find a match. It falls back to using the keys you provide initially, which is why it defaults to 'time' here.
That's all, happy high order component making!