Comments (5)
At work we do store components in the store, basically everything goes, but in the end there has got to be a reason for doing it. If you struggle coming up with one, i wouldn't do it.
from zustand.
I don't see a problem with having custom hooks in the state but I haven’t done that before.
In your example, the only way to load comments is through the hook. Let's say in the future you want to load comments when a user scrolls or clicks something. Now you have to add a loadComments
function anyway and your hook will probably just call loadComments
in useEffect
. At that point useLoadComments
would just be extra abstraction to save a few characters. In my opinion it's probably not worth it.
from zustand.
I would say it's not good in general, but it works as long as you don't violate rules of hooks. So, you can use the pattern with caution.
from zustand.
sometimes you want an action that would need a hook, or context, and in those cases, storing a hook that returns a function or an object would be very nice.
from zustand.
Hi, I don't see any issue in your snippet. Are you storing a hook in state?
As for the original issue, here's my current recommendation:
We don't intentionally document about storing hooks in Components or hooks, as it may confuse ordinary users.
The only valid use case I can think of is to lazy initialize a hook.
import create from 'zustand'
const Component = () => {
const useStoreRef = useRef()
if (!useStoreRef.current) {
useStoreRef.current = create(...)
}
const useStore = useStoreRef.current
...
}
You could use a state like:
const useStore = useState(() => create(...))
but it involves unnecessary state. One might want to consider useMemoOne
for shorthands.
from zustand.
Related Issues (20)
- Reset store not working on log out
- I can't update state with new data from Tanstack Query HOT 2
- The app hanging when initializing the store with and array of objects. HOT 4
- Is there any chance to simplify types?
- How to move the router state to store?
- Website graphics rendering issues (Linux) HOT 3
- shallow not support nested object HOT 1
- Are these two methods exactly the same? HOT 1
- It’s not recommended to suspend a render based on a store value returned by useSyncExternalStore.
- RFC: How is v5-alpha? HOT 11
- should useShallow add into the documents?
- Can't use persist with context implementation mentioned in the docs for Next.js HOT 1
- Re-render issue while performing open and setOpen using shadcn
- Re-render issue while performing open and setOpen using shadcn HOT 1
- type WithoutCallSignature<T>
- docs: Add suggestions about getInitialState on persist
- Cant user Array of Map type to store data
- overview of the zustand API HOT 1
- Persist storage : values are converted to strings before replacer
- Value is empty on initial render and sets after few milliseconds
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zustand.