hiebj / stately

elegant, type-safe state management
2 stars 1 forks source link

add HOC versions to Controllable and Async #24

Open hiebj opened 5 years ago

hiebj commented 5 years ago

The render prop pattern is great for most use cases, but sometimes it's necessary to have access to state, dispatch in the component lifecycle. Since render props only inject the references into the render() function, you'd currently have to wrap your lifecycle component to get access to those values:

const Wrapper = () =>
  <Subscriber>{
    (state, dispatch) =>
      <Wrapped state={state} dispatch={dispatch} />
  }</Subscriber>

class Wrapped extends React.Component {
  // I can now use state and dispatch in my lifecycle here
}

It would be more usable for these use cases if the components were also offered as HOC, allowing the user to simply decorate their existing lifecycle component rather than wrapping it.