Higher order component 與 Recompose
Higher order component
<WrappedComponent
injectedProp={injectedProp}
{...passThroughProps}
/>You can imagine that in a large app, this same pattern of subscribing to DataSource and calling setState will occur over and over again. We want an abstraction that allows us to define this logic in a single place and share it across many components. This is where higher-order components excel.
Recompose
const 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> )
Last updated