Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could you add something so that you can use the provider and the context in the same component? Maybe add an HOC like withContainer()? #82

Open
niamleeson opened this issue Jun 30, 2020 · 3 comments

Comments

@niamleeson
Copy link

niamleeson commented Jun 30, 2020

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);
@niamleeson
Copy link
Author

@jamiebuilds I just created a PR to fulfill the above ask. Please let me know if the PR doesn't meet your expectations. #83

@guiaramos
Copy link

guiaramos commented Jul 17, 2020

Work around might be the following, maybe:

import * as React from 'react';
import { createContainer } from 'unstated-next';

const useTeamState = () => {
  return {};
};

export const TeamStateContainer = createContainer(useTeamState);
export type TeamContainerState = ReturnType<typeof useTeamState>;

function withTeamContainer<P extends object>(Component: React.ComponentType<P>) {
  const WrappedComponent = (props) => {
    return (
      <TeamStateContainer.Provider>
        <Component {...props} />
      </TeamStateContainer.Provider>
    );
  };
  return WrappedComponent;
}

export default withTeamContainer;

@marcoskichel
Copy link

I use this function:

// connect.js
const connect = (Component, ...providers) => () =>
  providers.reduce(
    (children, Provider) => <Provider>{children}</Provider>,
    <Component />
  )

Then in the containers:

// myContainerComponent.jsx
export default connect(Welcome, SomeProvider, AnotherProvider)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants