DEV Community

Sathish A
Sathish A

Posted on

React Learning Journey – Day 3 Blog

Hello Everyone!!

Today’s session was really interesting because we learned about one of the most important concepts in React – Hooks. Let’s understand each topic step by step in a simple and clear way.

1.Introduction to Hooks:

Hooks are special functions in React. They let us "hook into" React features like state and lifecycle methods in functional components.
Before Hooks, only class components could use state, but now functional components can do everything with the help of Hooks!

2.What is a Hook?

A Hook is a JavaScript function that allows you to use React features (like state, context, refs) inside a functional component.

Note: Hooks only work in functional components, not in regular JavaScript functions or class components.

3.Why use Hooks? (Benefits of Hooks)

  • Easy to use state and lifecycle methods in functional components.
  • Avoid writing class components.
  • Code becomes cleaner, shorter, and more readable.
  • You can reuse stateful logic using custom hooks.

4.Types of Hooks

React provides some built-in hooks like:

  • useState()
  • useEffect()
  • useContext(), etc.

In today's class, we focused on useState()

5.What is useState()?

useState() is the most used React Hook.
It lets you add and manage state in a functional component.

Syntax:

const [stateVariable, setStateFunction] = useState(initialValue);
Enter fullscreen mode Exit fullscreen mode
  • stateVariable: To hold the data.
  • setStateFunction: To update the data.
  • initialValue: The starting value of the state.

6.Example of useState():

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0); // Initial value is 0

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • count is the state variable.
  • setCount is the function to update the count.
  • Initially, count = 0.
  • On button click, count increases by 1.

7.How to count value in JSX?

You can directly use state variables in JSX using {} brackets.

Example:

<h2>Count: {count}</h2>
Enter fullscreen mode Exit fullscreen mode

Whenever the count state updates, this value will automatically re-render in JSX.

8.Mini Project – Understanding Hooks

Today, we started a mini project (Counter App) using the useState() hook.

How to Understand?

  1. Break the problem into small parts.
  2. Think about what data (state) you need.
  3. Use useState() to store that data.
  4. Handle user actions (like button clicks) with functions.
  5. Render the state in JSX to see the result.

Recap Day 3:

  1. Introduction to Hooks.
  2. Why Hooks are important?
  3. useState Hook explained with example.
  4. How to count value in JSX.
  5. Started mini project – Counter App.

References:

[(https://legacy.reactjs.org/docs/hooks-intro.html)]

Learn Daily!! Build Better!! Stay Curious!!

Top comments (4)

Collapse
 
nevodavid profile image
Nevo David

Been cool seeing steady progress - it adds up. what do you think actually keeps things growing over time? habits? luck? just showing up?

Collapse
 
sathish_226_ profile image
Sathish A

Thank you! I believe it's all about showing up consistently and building good habits. Small steps make a big difference over time!

Collapse
 
dotallio profile image
Dotallio

Nice breakdown, especially on making useState feel simple! Did you try combining useState with useEffect yet in your mini project?

Collapse
 
sathish_226_ profile image
Sathish A

Thanks! Yes, I’ve started using useEffect with useState — super useful for handling side effects!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.