Hacker News new | ask | show | jobs
by lj3 2691 days ago
I do not imply the use of Context. useState() hooks into the local state of whatever component runs it. For example, if you have the following hook:

    function useMagicNumber(default) {
      const [magicNumber, setMagicNumber] = useState(default)

      return [magicNumber, setMagicNumber]
    }
And the following two components that use said hook:

    function ExampleA({}) => {
      const [magicNumber, setMagicNumber] = useMagicNumber(1)
      console.log(magicNumber) // 1
    }

    function ExampleB({}) => {
      const [magicNumber, setMagicNumber] = useMagicNumber(2)
      console.log(magicNumber) // 2
    }
ExampleA and ExampleB share the same code that's run in the hook useMagicNumber, but the state reflected is unique to the component.
1 comments

Then the confusion was "reuse state" and "share state". Hooks in themselves do neither. Provide primitives to build reusable behaviors. Yes. Solve issues classically considered the domain of Redux. Not on their own.