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
WOLFLEADER16mo ago
I've looked at slices, but they dont really solve the issue, as o still need the values from 1 store in anotehr
scot22
scot2216mo ago
I think your architecting something wrong, if you need both stores then why not combine them into one store?
WOLFLEADER
WOLFLEADER16mo ago
Because its used in more than 1 location. so one of them is my dbContext store for example
barry
barry16mo ago
dbContext? You have a db connection on the frontend? I don’t get it, that doesn’t sound like a good idea
scot22
scot2216mo ago
So then both locations depend on that one store
WOLFLEADER
WOLFLEADER16mo 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)
scot22
scot2216mo 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
WOLFLEADER16mo ago
Right now to access dbContext inside of readerStore i do dbStore.GetState().context
scot22
scot2216mo ago
I wonder if this is also a case of storing too many things within the store
WOLFLEADER
WOLFLEADER16mo ago
i dont believe so the readerStore contains the logic for the native module
isaac_way
isaac_way16mo ago
Zustand has a .getState method on the store hook you can use to get the state outside of components
WOLFLEADER
WOLFLEADER16mo ago
Yea that’s what I’m using atm But is doing that a sign of bad architecture?
isaac_way
isaac_way16mo 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
WOLFLEADER16mo 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_way16mo 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
WOLFLEADER16mo 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_way16mo ago
yeah sounds kind of like a shit show haha did it fix the performance?
WOLFLEADER
WOLFLEADER16mo 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_way16mo ago
dannggg haha a non react dev ends up with rendering performance issues A tale as old as time
WOLFLEADER
WOLFLEADER16mo ago
Yep 😂😂 seem to have fixed it 🤣
isaac_way
isaac_way16mo ago
@WOLFLEADER NICEEE. probably was just like rerendering the entire app constantly with the contexts lol
WOLFLEADER
WOLFLEADER16mo ago
Yea lol I think eventuaklly I’ll fix up the architecture too haha
Want results from more Discord servers?
Add your server
More Posts
"The AWS Access Key Id you provided does not exist in our records."..but I have the right access keyTrying to use the S3 SDK. ```js export const s3 = new S3({ region: env.REGION, credentials: { Build failed due to import error`Build failed due to import error` After running the build command for my react app the build failNextAuth auth flow causes Next.js 13 dev server to crash without throwing any errorsI'm building a note-taking app with ct3a (partly inspired by Jack Herrington's awesome T3 video) butCustomize QueryClient for TRPC?👋 I’m using T3 for a project. Is it possible to pass a custom QueryClient to TRPC? I don’t like NextAuth credentials how to handle unauthorized user?I have this parent component which distinguishes if the user is logged in or not and redirect the usAnyone Have A Recommended Markdown Editor That They Use That Works With Next?I was going to use react-md-editor (https://github.com/uiwjs/react-md-editor) however it seems that WebSocket connection to 'ws://localhost:3001/' failed:i have been trying to get websocet to work on t3 stack the past 6 hours but still unable to get it tCRON jobs in the T3 stackHey everyone, I’ve got a project at work that is going to require the use of some CRON jobs. My baAurora Serverless V2?I’m currently looking into using this for a Postgres db and Prisma. Does anyone have experience withCan you critique my idea/methodology for a Tailwind UI lib + semantic color system combo?I am still a pretty entry level dev (~2 years experience), and this is my first UI library. While I