Error: Store with name [state name] already exists
carl-angelo opened this issue · comments
Hi, I am having this error when I tried to use the store state that I created.
`import { createStore, useStore } from 'react-hookstore';
export const storeState = (entity, initState, reducer) => {
let states = {};
if (states[entity]) return states[entity];
return states[entity] = createStore(entity, initState, reducer)
}
export const useSidebarState = () => useStore(storeState('sidebar', true));`
This error is only displayed when you try to call createStore
with the same store name more than once.
Your code is doing exactly that because your states
variable is being recreated every time you run the function storeState
move the line let states = {}
to the outside of the function in order to correctly persist the state
Please let me know if this solves your problem :)
This error is only displayed when you try to call
createStore
with the same store name more than once.Your code is doing exactly that because your
states
variable is being recreated every time you run the function you created.move the line
let states = {}
to the outside of the function in order to correctly persist the statePlease let me know if this solves your problem :)
Get the line 4 of this example and move it to be line 2, as I explained before.
I'm closing this since no activity for one month. But I'm sure the issue is being caused by what I pointed so it's fine!