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)
- 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
- Immer Middelware Typescript error: Exported variable 'useMyStore' has or is using name 'WritableDraft' from external module "../node_modules/immer/dist/immer" but cannot be named.
- blacklist and whitelist
- Argument of type '(state: ClientStore) => Client | undefined' is not assignable to parameter of type 'ClientStore | Partial<ClientStore> | ((state: ClientStore) => ClientStore | Partial<ClientStore>)'. Type '(state: ClientStore) => Client | undefined' is not assignable to type '(state: ClientStore) => ClientStore | Partial<ClientStore>'. Type 'Client | undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'. Type 'undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'.ts(2345)
- The value I save to storage with persist is delayed when the page refreshes.
- State updated in one component but not in another one
- Typescript + Slices = trouble HOT 7
- Setting nested state make the other states change (their values didn't change)
- zustand concurrent update issue
- Typo in GitHub "About" HOT 1
- import { create ,WritableDraft} from "zustand" HOT 1
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.