Recompose library - Tuong-Nguyen/JavaScript-Structure GitHub Wiki
Recompose is a React utility belt for function components and higher-order components. Think of it like lodash for React.
npm install recompose --save
compose(HOCs): Use to compose multiple higher-order components into a single higher-order componentmapProps(): Maps the current props and return "derived" props to base componentwithState(): Help the react component stays dumb but has ability to usestatewithHandlers(): Avoids create new handler on every render.lifecycle(): Access to component lifecycle methods- Read more
mapProps(
propsMapper: (ownerProps: Object) => Object,
): HigherOrderComponentcompose(
withRouter,
graphql(...),
mapProps(({ data, ...rest }) => {
const jobs = get(data, 'jobsByEmployerId.jobs');
const total = get(data, 'jobsByEmployerId.total');
return {
ready: !data.loading,
listData: jobs,
pageCount: Math.ceil(total / 20),
...rest,
};
})
)-
mapProps()receivesdatafromgraphqlHOC and populate the new propslistDatafrom data'sjobsByEmployerId.jobs,pageCountfrom data'sjobByEmployerId.totaland keep the rest ofpropsto base component
withState(
stateName: string,
stateUpdaterName: string,
initialState: any | (props: Object) => any
): HigherOrderComponent- Add 2 prop to
propsof base component:- a state value that has state name is
stateName - a function that has name is
stateUpdaterNameto update the state value
- a state value that has state name is
- The state updater has 2 signatures:
stateUpdater<T>((prevValue: T) => T, ?callback: Function): void
stateUpdater(newValue: any, ?callback: Function): voidconst enhance = withState('counter', 'setCounter', 0);
const Counter = enhance(({ counter, setCounter }) =>
<div>
Count: {counter}
<button onClick={() => setCounter(n => n + 1)}>Increment</button>
<button onClick={() => setCounter(n => n - 1)}>Decrement</button>
</div>
);withHandlers(
handlerCreators: {
[handlerName: string]: (props: Object) => Function
} |
handlerCreatorsFactory: (initialProps) => {
[handlerName: string]: (props: Object) => Function
}
): HigherOrderComponent- Handlers are expected a map of higher-order functions
withHandlers({
onChange: props => event => {
//...
},
onSubmit: ...
})const addCounting = compose(
withState('counter', 'setCounter', 0),
withHandlers({
increment: props => () => props.setCounter(n => n + 1),
decrement: ({ setCounter }) => () => setCounter(n => n - 1),
reset: ({ setCounter }) => () => setCounter(0)
})
)
const Counter = addCounting(({ counter, increment, decrement, reset }) =>
<div>
Count: {counter}
<br/>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
<button onClick={reset}>Reset</button>
</div>
);lifecycle(
spec: Object,
): HigherOrderComponent- Supports entire
ComponentAPI, exceptrender()(overridden if specified) - Using
setStateto make state changes that will be propagated to the wrapped component as props.
lifecycle({
componentDidMount() {
const { checkIfAuthed } = this.props;
// Do they have an active session? ("Remember me")
checkIfAuthed();
},
//...
})