question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ Question ] How to make a store instance per Component ?

See original GitHub issue

let’s say you have a <Button /> component which some how stores it’s state in zustand with buttonStore store.

now you want each button to have it’s own state / store ( zustand store/state )

how would you achieve that ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

16reactions
drcmdacommented, Aug 13, 2020

it’s not 100% clear to me what you mean, but the store can be created everywhere, it doesnt violate rules of hooks because it is not a hook, it merely returns one.

it’s just that it’s now being the users responsibility to distribute it, because its not a global singleton any longer but has a lifecycle. this is where context is probably best. but you could also do prop drilling if you wanted.

store.js

const context = createContext()
const useStore = (selector) => useContext(context)(selector)

function Store({ children }) {
  const [useStore] = useState(() => create(...))
  return (
    <context.Provider value={useStore}>
      {children}
    </context.Provider>
  )
}

export { Store, useStore }

now every component inside Store can use the hook.

app.js

import { Store } from "./store"

function App() {
  return (
    <Store>
      <Foo />
    </Store>
  )
}

foo.js

import { useStore } from "./store"

function Foo() {
  const bar = useStore(state => state.bar)
  ..

and just like that we have a dynamic, component bound store that is lifecycle-safe. i guess we have re-created react-redux with the provider and all 😛

8reactions
boltcodercommented, Mar 1, 2021

Funny thing -> I was using react context providers and then was context bridging to work with react-three-fiber. Then I moved everything off from context and moved to zustand. Life was good. Now I need multiple instances of my 3d component. So I need to provide the zustand store I create via contextproviders bridged between three-fiber’s canvas and normal react-dom. Thumbsup if you’re all coming full circle like me 👯

PS: I still love this library. You guys are great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[ Question ] How to make a store instance per Component
One use case is that we want to create a store in React tree, and then pass it with props or context. This...
Read more >
How to create one store per instance in Redux? - Stack Overflow
I wonder how to dispatch actions to the component's own special store? Is there a way to access the store -prop of the...
Read more >
React Components, Elements, and Instances – React Blog
We let React create, update, and destroy instances. We describe them with elements we return from the components, and React takes care of ......
Read more >
How To Create Custom Components in React - DigitalOcean
In this tutorial, you'll learn to create custom components in React. Components are independent pieces of functionality that you can reuse ...
Read more >
How get lightning-tree to render separate component instance ...
TLDR - iterate instances that I wanna render and dynamically update their visibility. Keep cache of already rendered components and use css ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found