jhonnymichel / react-hookstore

A state management library for react using the bleeding edge hooks feature

Home Page:https://codesandbox.io/s/r58pqonkop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 is the error message:
image

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 state

Please let me know if this solves your problem :)

Hi! I am creating the store at this way
image

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!