Zustland Multiple stores

hey when using something like zustand, how would i use a store inside another store? for example i have metaDataStore which contains metadata, that i want to use in my readerStore metadata is getting used in a callback in the readerStore. The readerStore contains an event callback for onRead() so i cant really pass it into the function
22 Replies
WOLFLEADER
WOLFLEADER2y ago
I've looked at slices, but they dont really solve the issue, as o still need the values from 1 store in anotehr
Scot
Scot2y ago
I think your architecting something wrong, if you need both stores then why not combine them into one store?
WOLFLEADER
WOLFLEADER2y ago
Because its used in more than 1 location. so one of them is my dbContext store for example
barry
barry2y ago
dbContext? You have a db connection on the frontend? I don’t get it, that doesn’t sound like a good idea
Scot
Scot2y ago
So then both locations depend on that one store
WOLFLEADER
WOLFLEADER2y ago
its a react native app its a local sqlite file yea so the way i have it is i have dbStore metaDataStore readerStore reader interacts with custom hardware dbstore is for the db context, and meta data is data which is from the database (not my naming, they had these names before i jumped in, but i should rename them)
Scot
Scot2y ago
Ah can you just duplicate the data into the db store as a quick fix And then maybe see if it needs to be in meta data Having a store within a store seems wrong to me
WOLFLEADER
WOLFLEADER2y ago
Right now to access dbContext inside of readerStore i do dbStore.GetState().context
Scot
Scot2y ago
I wonder if this is also a case of storing too many things within the store
WOLFLEADER
WOLFLEADER2y ago
i dont believe so the readerStore contains the logic for the native module
isaac_way
isaac_way2y ago
Zustand has a .getState method on the store hook you can use to get the state outside of components
WOLFLEADER
WOLFLEADER2y ago
Yea that’s what I’m using atm But is doing that a sign of bad architecture?
isaac_way
isaac_way2y ago
@WOLFLEADER yeah so u can use that to access a store from another store Probably depends on the circumstance Does sound like it would be hard to follow though
WOLFLEADER
WOLFLEADER2y ago
The issue I have is the previous app was designed in a way that everything was basically global state in the app component and passed down through resct contexts But that was causing performance hits. I’m coming in and trying to fix that But I might need to sit down and really think about the architecture here To me it makes sense to keep the native module singleton in a store. But maybe I should extract the logic thst interacts with the database out of it It’s also annoying as the native module has events I need to listen too in the app. So right now I listen to them in the store, then update a value to cause a useEffect to run so that you know it’s been called
isaac_way
isaac_way2y ago
if it’s just a bunch of contexts that are causing rendering issues then simply converting everything to zustand and using selectors would probably make the performance good, it’s pretty low effort compared to restructuring everything. Could start with the conversion to zustand then think about redesigning things afterwards once it’s working and the perf is fixed. Native module as in react native native module?
WOLFLEADER
WOLFLEADER2y ago
Yea react native modules And yep that’s what I’ve done. Just moved them all into their own zustand stores But then ye some of them require the others
isaac_way
isaac_way2y ago
yeah sounds kind of like a shit show haha did it fix the performance?
WOLFLEADER
WOLFLEADER2y ago
Haha haven’t been able to test yet as it’s a shit show lmao I’ll be testing it today The people who made theee changes to the app aren’t react devs lmao And I know that because some of the state was stored in app and passed down as props lmao
isaac_way
isaac_way2y ago
dannggg haha a non react dev ends up with rendering performance issues A tale as old as time
WOLFLEADER
WOLFLEADER2y ago
Yep 😂😂 seem to have fixed it 🤣
isaac_way
isaac_way2y ago
@WOLFLEADER NICEEE. probably was just like rerendering the entire app constantly with the contexts lol
WOLFLEADER
WOLFLEADER2y ago
Yea lol I think eventuaklly I’ll fix up the architecture too haha
Want results from more Discord servers?
Add your server