Clarification on using getState() vs useStore and potential side effects #2712
-
Hi, I have some questions to ask you: 1. Difference Between useStore and getState() for Accessing FunctionsI’ve been discussing the usage of useStore versus getState() for accessing functions within the store. For example:
It’s clear that useStore is typically used to subscribe to state changes, ensuring that components re-render when the state updates. However, when using useStore to access a function like addTodo, what are the practical differences from using getState() directly, given that functions do not change and do not trigger re-renders? 2. Potential Side Effects or Risks of Using getState().aFunction Outside a React ComponentIs there any risk or are there any potential side effects when calling a function from the store using 3. Potential Side Effects or Risks of Using getState().state Outside a React ComponentSimilarly, are there any risks or potential side effects when accessing a piece of state directly using Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@maurosaladino there're not differences between using |
Beta Was this translation helpful? Give feedback.
@maurosaladino yeah, sure. The only different between accessing state using
getState()
anduseStore(...)
in React is that you could end up having extra re-renders by accessing to the state usinggetState()
, in the other hand you could improve your perf by memoizing selectors usinguseStore(...)