You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it good or bad to have stores talk to each other? If it's ok, what would be good/bad ways of going about it? E.g. Here's one idea, if stores do use other stores, they could use the exposed API instead of the hook. I'm making this use-case up, so it might be unrealistic.
// UsersStore.jsexportconst[useUsersStore,usersStore]=create(...)// CommentsStore.jsimport{usersStore}from'./UsersStore.js'exportconst[useCommentsStore,commentsStore]=create(set=>({asyncloadUserComments(userId){// Note: I'm accessing the other store hereconstcommentIds=usersStore.getState().users[userId].commentIdsset({comments: awaitfetch(`/comments`,{ids: commentIds})})}}))
Or would it be better to force components to compose stores together like this?
// UsersStore.jsexportconst[useUsersStore]=create(...)// CommentsStore.jsexportconst[useCommentsStore]=create(set=>({comments: [],asyncloadComments(commentIds){set({comments: awaitfetch(`/comments`,{ids: commentIds})})}}))// Component.jsxfunctionUserComments({ id }){// Note: I'm doing all the wiring together in the component insteadconstcommentIds=useUsersStore(state=>state.users[id].commentIds)constcomments=useCommentsStore(state=>state.comments)constloadComments=useCommentsStore(state=>state.loadComments)useEffect(()=>{loadComments(commentIds)},[commentIds])}
Maybe, if you really want to encapsulate the logic, you could create a custom hook to wire them together? We could export an additional non-zustand custom hook that joins two (or more) zustand stores together to provide composited functionality.
Would this even work? I think I like this...
// Not sure what file I'd put this in ¯\_(ツ)_/¯exportconstuseUserComments=(userId)=>{constcommentIds=useUsersStore(state=>state.users[id].commentIds)constcomments=useCommentsStore(state=>state.comments)constloadComments=useCommentsStore(state=>state.loadComments)useEffect(()=>{loadComments(commentIds)},[commentIds])returncomments}// Component.jsxfunctionUserComments({ id }){constcomments=useUserComments(id)}
The text was updated successfully, but these errors were encountered:
Is it good or bad to have stores talk to each other? If it's ok, what would be good/bad ways of going about it? E.g. Here's one idea, if stores do use other stores, they could use the exposed API instead of the hook. I'm making this use-case up, so it might be unrealistic.
Or would it be better to force components to compose stores together like this?
Maybe, if you really want to encapsulate the logic, you could create a custom hook to wire them together? We could export an additional non-zustand custom hook that joins two (or more) zustand stores together to provide composited functionality.
Would this even work? I think I like this...
The text was updated successfully, but these errors were encountered: