jamiebuilds / unstated-next

200 bytes to never think about React state management libraries ever again
MIT License
4.18k stars 145 forks source link

Add withContainer hoc #83

Open niamleeson opened 4 years ago

niamleeson commented 4 years ago

Here's a use case. I think the API would be really nice.

import { State } from './state.provider';

const Parent = () => {
    const { handleAction, currState, component: Component } = State.useContainer();

    return (
          <Component sendAction={handleAction} pollData={pollData} />
    );
};

export default State.withContainer(Parent);

Without the above, you need to do this everytime you create a new state:

import { State } from './state.provider';

const withContainer = (Component) => (props) => (
    <State.Provider>
        <Component {...props} />
    </State.Provider>
);

const Parent = () => {
    const { handleAction, currState, component: Component } = State.useContainer();

    return (
          <Component sendAction={handleAction} pollData={pollData} />
    );
};

export default withContainer(Parent);