-
|
Hi i was wondering if some one can help me at explain me ho to sync datas in a store between multiple tabs, preferably with an example. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 10 replies
-
|
You just need to search the internet about that. There are multiple possibilities for communication. |
Beta Was this translation helpful? Give feedback.
-
|
Just searched for "zustand broadcast channel" and found https://github.com/Tom-Julux/shared-zustand. |
Beta Was this translation helpful? Give feedback.
-
|
Yep, i foudn that too yesterday but as I read in the docs it's in a very primordial state but it can be useful as an example an take some inspiration to do something better, thanks @dai-shi, may be i can start thinking o writing some code to integrate to zustand in order to let users do this kind of things without do that from scratch, what do you think? |
Beta Was this translation helpful? Give feedback.
-
|
does anyone know if there is a ready-to-use library to achieve client-side state synchronization using Zustand and Websockets ? on the lines of https://blog.armanddu.com/real-time-app-with-react-and-web-sockets-part-2/ |
Beta Was this translation helpful? Give feedback.
-
|
#1614 (comment) |
Beta Was this translation helpful? Give feedback.
-
|
It's actually easy to write a middleware for this: Middleware: Usage: I've also made this into a npm package here: (zero dependencies) Usage: Demo: https://zustand-sync.netlify.app/ |
Beta Was this translation helpful? Give feedback.
-
|
Here's my approach, intercept /**
* Wraps a Zustand set function to sync state across windows using BroadcastChannel.
* @param set The original Zustand set function
* @param channelName The BroadcastChannel name
* @returns The wrapped set function
*/
export function syncSet<T extends object>(set: (updater: T | ((state: T) => T)) => void, channelName: string): typeof set {
const syncChannel = new BroadcastChannel(channelName)
// Listen for messages from other windows to update the store state
syncChannel.onmessage = (event) => set(event.data)
// Return a new set function that broadcasts state changes
return (updater) => {
if (typeof updater === 'function') {
set((oldState: T) => {
const newState = updater(oldState)
const serializableState = filterSerializableState(newState)
syncChannel.postMessage(serializableState)
return newState
})
} else {
set(updater)
const serializableState = filterSerializableState(updater)
syncChannel.postMessage(serializableState)
}
}
}
/**
* Filters out non-serializable properties from an object for BroadcastChannel transmission
*/
function filterSerializableState<T extends object>(state: T): Partial<T> {
const filtered = {} as Partial<T>
for (const [key, value] of Object.entries(state)) {
// Skip AbortController and other non-serializable objects. You may update this to fit your usage.
if (value instanceof AbortController) continue
if (typeof value === 'function') continue
if (value === undefined) continue
filtered[key as keyof T] = value
}
return filtered
}usage: const useStore = create(
persist(
(set) => {
+ set = syncSet(set, 'preference-store-broadcast-channel')
return { /* ... */ }
},
persistOptions
)
); |
Beta Was this translation helpful? Give feedback.
You just need to search the internet about that. There are multiple possibilities for communication.
And none of them directly relates to the zustand. So I believe that your question is out of scope.