Git Product home page Git Product logo

Comments (5)

drcmda avatar drcmda commented on May 3, 2024

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.

JeremyRH avatar JeremyRH commented on May 3, 2024

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.

dai-shi avatar dai-shi commented on May 3, 2024

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.

yehonadav avatar yehonadav commented on May 3, 2024

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.

image

from zustand.

dai-shi avatar dai-shi commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.