Hooks were first introduced in React v.16.8, so that we could use state and other React features without writing a class.
Some of the built-in ones include useState, useEffect, useRef, useCallback, useMemo, useContext and useReducer.
Which ones do you use the most? And how often do you write your own custom hooks?
              
    
Top comments (9)
I use
useStateanduseEffectin most components, though I do try to avoid using hooks if I can help it, just for the performance gains.I've written a few custom hooks I tend to only write them if they're gonna be used in more than one place in the app I'm working on or if I can see using the hook in several apps in the future. If it's one off functionality I can just embed the logic into the component that needs it.
useState,useEffectanduseContextfor Firebase integration.Sometimes
useMemofor expensive computations (deriving a state for instance).Otherwise, never happen to use more than that as for now.
useRef(initialValue)is justuseState({ current: initialValue })[0], so you get that one for free.useCallback(callback, dependencies)is merely a shorthand foruseMemo(() => callback, dependencies). Also, you forgotuseLayoutEffectin your list.I mostly use
useState,useRefanduseEffect. If need be, I'll useuseContext, but I try to avoiduseCallback(you can directly use a Dispatch from useState as a callback in most occassions and handle the result in an effect). I also mostly refrain from usinguseReducer, as it is meant to provide an elegant abstraction of complex logic - and one should always simplify logic as much as possible before abstracting it into code.I'm hooked! Using all the build-in hooks quite often.
useStateanduseEffectthe most of course. Also writing a lot of custom hooks. Once I find myself re-using a lot is for exampleuseQueryParams(with theqspackage),useMediaQuery,useModaluseStateby far.Since i have to pick only one it is definitely useState π
Building custom hooks depends on the size of project and reusability.
Mostly I use
useStateanduseEffectwith or Without dependency array. Also will useuseRef,useCallbackanduseMemoat required scenarios.Never write any Custom Hooks, instead the first option come to mind is writing custom utility functions as we working on Big project. π
useState, useEffect and useContext are used in most components.
Sometimes i use useReducer to handle complex state logic and build better unit tests
useState, useEffect and useRef