DEV Community

TenE
TenE

Posted on

React Hooks Overview

Hook Purpose Basic Usage
useState Adds state to function components const [count, setCount] = useState(0);
useEffect Performs side effects in function components useEffect(() => { console.log(count); }, [count]);
useContext Accesses values from a React context const value = useContext(MyContext);
useReducer Manages complex state logic const [state, dispatch] = useReducer(reducer, initialState);
useCallback Memoizes a callback function const memoizedFn = useCallback(() => { doSomething(); }, [dep]);
useMemo Memoizes a computed value const memoizedValue = useMemo(() => computeExpensiveValue(dep), [dep]);
useRef Returns a mutable reference const inputRef = useRef(null);
useImperativeHandle Customizes the instance exposed by ref useImperativeHandle(ref, () => ({ focus: () => inputRef.current.focus() }));
useLayoutEffect Runs synchronously after DOM mutations useLayoutEffect(() => { console.log('Layout effect'); });
useDebugValue Adds debug labels to custom hooks useDebugValue(isOnline ? 'Online' : 'Offline');
Custom Hooks Extracts reusable logic into a function function useFetch(url) { useEffect(() => { fetch(url); }, [url]); }

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay