aweary / react-copy-write

✍️ Immutable state with a mutable API
MIT License
1.79k stars 54 forks source link

share state instance in multiple components #11

Closed vesparny closed 6 years ago

vesparny commented 6 years ago

Hi, what is the recommended way to create the state and re-use it across components in different files? Imagine in your app.js you create the state

const State = createState({
  user: null,
  loggedIn: false
});

How do I use State and Providers in connected components?

I worked on a lib called statty and one of the most annoying thing is to create a wrapper component when you need the update in lifecycle hooks

  <State
    select={selector}
    render={({ count }, update) => (
      <ComponentsThatNeedsToAccessUpdateInLifecycle update={update} />
    )}
  />

createMutator seems meant to solve this pain, and I wonder what is the suggested way to use it in multiple components :)

Thanks

aweary commented 6 years ago

If you need to update state in a lifecycle, using createMutator is definitely the recommended way to do it!

vesparny commented 6 years ago

Assuming I create my State instance in a top-level component, should I pass createMutator as a prop to inner components?