Menu

What are Hooks in React?
πŸ“°

What are Hooks in React?

DEV Community: reactΒ·SATHISHKUMAR RΒ·4 days ago
#bBOTql81
#devtoreact#devto#dev#react#usestate
Reading 0:00
15s threshold

React Hooks are special functions that let us use React features in functional components.

For Example:

Store and update data.

Handle side effects.

Access other React features. Common React Hooks are useState, useEffect, useContext, and useRef.

useState Hook:

useState is used to store and update a value.

Example:

Counter app.

import { useState } from "react";

function App() {

const [count, setCount] = useState(0);

return (

Count: {count}

setCount(count + 1)}>

Click Me

);

}

export default App;

Try the counter

0

βˆ’ Decrease+ IncreaseReset

Explanation:

useState(0) β€” initializes the count to 0.

count β€” stores the current value.

setCount β€” updates the value.

onClick β€” runs when the button is clicked.

2.Common React Hooks:

Hook Purpose

useState Store and update data

useEffect Perform side effects, such as fetching data

useContext Access shared data

useRef Keep a reference to a value or DOM element

useMemo Cache a calculated result

useCallback Cache a function definition

useEffect…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.