[question] whether can observe a state under zustand store, not in a component? #2957
Answered
by
dbritto-dev
liaodalin19903
asked this question in
Q&A
-
I have a requirement: I want to observe the state of zustand store, and when it change I can do something else. we know we can use subscribe to listen a state changes(code is below), but I want to know whether this listen function can implement in store(not in a react component)? The detail code: src/store/testSliceStore.ts
usage:
|
Beta Was this translation helpful? Give feedback.
Answered by
dbritto-dev
Jan 15, 2025
Replies: 1 comment 3 replies
-
@liaodalin19903 sure, instead of const store = createStore()
store.subscribe(...)
const Component = () => {
const state = useStore(store)
useEffect(() => {
const unsubscribe = store.subscribe(...)
return () => {
unsubscribe()
}
}, [])
return null
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@liaodalin19903 that's it