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

Feature: Use overmind for isolated feature development #477

Open
suevalov opened this issue Jan 13, 2021 · 0 comments
Open

Feature: Use overmind for isolated feature development #477

suevalov opened this issue Jan 13, 2021 · 0 comments

Comments

@suevalov
Copy link

First of all, thanks for creating and maintaining Overmind.

We're happy users of it and we like the concepts and excellent Typescript support but as our application grows we find more and more use cases when we don't want to keep the state in a global store and we switch to using React.Context + hooks for isolated feature development, so we can even pack the feature as an NPM package at some point and reuse it between several applications.

Switching between the local state with hooks and the global store with Overmind has a cognitive overhead since the concepts are different. When we realize that it's better to keep some particular state locally, or globally, moving an implementation from local to global, or from global to local requires a substantial refactoring.

It would be nice if we could use Overmind for local feature development, so moving actions, states, reactions, and effects between local and global is just a matter of moving code blocks.

Something similar to useLocalStore in Mobx would be extremely useful in this case. An ability to create a local instance without a lot of boilerplate would encourage to start with a local store and move things to a global one if when necessary.

import { createLocalOvermind } from 'overmind-react'

const [ MyFeatureProvider, useMyLocalFeature ] =  createLocalOvermind('my-unique-feature-id', (initialState) => {
  return {
     actions: {
        setFoo({ state }, value) {
           state.foo = value;
        }
     },
     state: {
        foo: initialState.foo
     },
     effects: {},
     onInitialize: function () {},
     // this is called when Provider is remove from React tree
     onDispose: function () {}
  };
})

export { MyFeatureProvider, useMyLocalFeature }; 


const Container = () => {
   return (
      <MyFeatureProvider foo="bar">
         <ChildComponent />
     </MyFeatureProvider>
   );
}


const ChildComponent = () => {
   const feature = useMyLocalFeature();
   return (
     <div>
       <button onClick={() => { feature.actions.setFoo('batman')}}>change</button>
       <span>{feature.state.foo}</span>
     </div>
    );
};
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

1 participant