Hooks in React
Why you need hooks in react

đź‘‹ Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!
To understand the scenario of why you need hooks in React, you must this video completely. I am jumping directly to the keynotes, but make sure you also watch Hitesh sir's storytelling.
Building a Counter Project
import { useState } from 'react'
import './App.css'
function App() {
let counter = 15
const addValue() {
console.log(counter)
counter = counter + 1;
}
const removeValue() {
console.log(counter)
counter = counter - 1;
}
return (
<>
<h1>Chai aur React</h1>
<h2>Couter value: {counter}</h2>
<button
onClick={addValue}
>Add Value {counter}</button>
<br />
<button
onClick={removeValue}
>Remove Value {counter}</button>
<p>{counter}</p>
</>
)
onClick={addValue}:addValueis used instead ofaddValue()because we want to execute this function when we click on the button.addValue()can be automatically executed without clicking the button, so we cannot use this.
I faced some issues with UI updating. The value of the counter updates in the console but not in the UI. Both buttons are working, but the main issue, which is UI updating, cannot be resolved.
We cannot change UI by directly changing the variable in React because React controls UI updation.
This problem is addressed by using a React Hook—specifically, the useState Hook.
How to use useState Hook
useStatetakes a default value. The value can be of any type: Number, Boolean, Empty String, Array, Object, Null, etc.Syntax:
const [counter, setCounter] = useState(defaultValue);useStateprovides two things:The current value of the
counter.A method that controls the
countervariable. The method is namedsetCounter; however, you can choose any suitable name likechaiCounter, etc. Conventionally,setCounteris a good choice.
What useState does?
- This hook changes the state. The change can propagate to the UI, unlike
let counter = 15, which will never propagate to the UI.
How to update the counter
setCounter(counter + 1)will update thecountervalue.
Rebuilding a Counter Project
import { useState } from 'react'
import './App.css'
function App() {
let [counter, setCounter] = useState(15);
const addValue = () => {
setCounter(counter + 1);
}
const removeValue = () => {
setCounter(counter - 1)
}
return (
<>
<h1>Chai aur React</h1>
<h2>Couter value: {counter}</h2>
<button
onClick={addValue}
>Add Value {counter}</button>
<br />
<button
onClick={removeValue}
>Remove Value {counter}</button>
<p>{counter}</p>
</>
)
}
export default App
Assignment Solution
Assignment: Counter cannot be less than 0 and greater than 20.
function App() {
let [counter, setCounter] = useState(15);
const addValue = () => {
if (counter < 20) {
setCounter(counter + 1);
}
}
const removeValue = () => {
if (counter > 0) {
setCounter(counter - 1)
}
}
}




