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

If "createStore" is called multiple times with the same storename, got werid errors that the browser keeps saying that it cannot produce stack trace.

pencilcheck opened this issue · comments

I'm using nextjs, and the server will recompile in realtime, it seems like after recompile, the createStore is treated as calling multiple times, and when I make any changes, the server will start emitting error.

In the server side, keep getting
"store already exists" message

and on the client side, the inspector keep saying

index.js:2370 Uncaught Error: The error you provided does not contain a stack trace.
    at parseError (index.js:2370)
    at getStackFrames (index.js:2263)
    at crashWithFrames (index.js:1927)
    at index.js:1945
    at rejectionHandler (index.js:2041)
parseError @ index.js:2370
getStackFrames @ index.js:2263
crashWithFrames @ index.js:1927
(anonymous) @ index.js:1945
rejectionHandler @ index.js:2041
23:22:01.348 hot-dev-client.js:131 Uncaught (in promise) Error

Ah, didn't know that createStore throws an error if the store already exists... that's a weird thing to do.

I just wrap it in try catch and the app starts working again...

Looks like wrapping in try catch doesn't work...

If I now with wrapped createStore, if I make any changes, nextjs recompiles, then I get
store does not exist in the code I used for useStore

Found out that it was because of duplicated createStore names used.

@pencilcheck I guess I'll remove the error and switch it with an warning...

Probably, raising the error from the library doesn't really bubble up properly.

@jhonnymichel why not treat the stores as singletons? I'm experiencing similar issues (warning in the nextjs console) about the Store already existing. My naive assumption is that createStore should just create it once. You can even take this further and not require createStore at all and that if useStore('myStore') accesses a store that doesn't exist yet, just silently create it and then use it.

The idea is to allow multiple stores. It is not the same approach as lets say, redux.

But I'll debug what is going on with nextjs. even with SSR, it doesn't make sense to have the same createStore command being called more than once.

But if I did this:

createStore('my-store');
createStore('my-store');
createStore('your-store');

I'd expect only 2 stores to be created: 1x my-store, 1x your-store. Still multiple stores, but not a duplicate my-story.

Hello, really nice library. Any updates on this issue, I am getting the same error with NextJs usage. Maybe it would be great to remove "throw error" in getStoreByName, just return null if not found. This will simplify adaptation.
My current workaround is

const getStore = (name, initState, reducer) => {
  try {
    return createStore(name, initState, reducer)
  } catch (err) {
    return getStoreByName(name)
  }
}

@PavelZiber Today's release removes the getStore error.

fixed in 1.5.0