Difference between useStore
and store.getState()
#2194
-
IntroductionI have been using My store has a data field of type QuestionAre there any difference or drawback when using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I used useMyStore.getState() in a react component to just get the value for each render but not subscribe it. However, I've just installed React Compiler and its eslint. It warns me: And when I
I don't know why my components increase from 107 to 108. To conform with React Compiler, we should not use .getState() directly in React components. Instead, we should call the store as a hook. This will trigger re-renders when the state changes, but auto memoization from React Compiler should help optimise performance (need to test further). Update:
|
Beta Was this translation helpful? Give feedback.
store.getState()
is to read the store state in vanilla. You need to useuseStore
to have a reactive value in React. In other words, ifstore.getState()
just works, it's good to go.