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]); }

Image of Datadog

Optimize UX with Real User Monitoring

Learn how Real User Monitoring (RUM) and Synthetic Testing provide full visibility into web and mobile performance. See best practices in action and discover why Datadog was named a Leader in the 2024 Gartner MQ for Digital Experience Monitoring.

Tap into UX Best Practices

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay