Cookie Consent by Free Privacy Policy Generator 📌 Mastering React Hooks: A Comprehensive Guide to Functional Components

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Mastering React Hooks: A Comprehensive Guide to Functional Components


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Introduction

The introduction of Hooks in React 16.8 significantly changed the coding style for building React components.
Hooks allow to control component's state, lifecycle and offer a more powerful and flexible way to create and manage functional components.

Prior to Hooks, stateful components required the use of classes, which could complicate the codebase and increase a learning curve for new frontend developers.
Hooks provide a way to "hook into" React state and lifecycle features from function components.
In this blog post you will learn the essentials and the most common React Hooks.

useState hook

useState allows to save and update data inside the functional component that persists through multiple component re-renders.

import { useState } from "react";

export const CoffeeCounter = () => {
    const [count, setCount] = useState(0);

    return (
        <div>
            <p>You have {count} coffee(s)</p>
            <button onClick={() => setCount(count + 1)}>
                + 1 coffee
            </button>
            <button onClick={() => setCount(count - 1)}>
                - 1 coffee
            </button>
        </div>
    );
}

Here we define a count variable inside a CoffeeCounter component.
The useState hook contains a variable itself and a function that allows to a change value of the variable.
Everytime a setCount function is called - a component is rendered with a new value of count variable.

In this example we created a simple counting component that increases and decreases count by 1 every time you press a corresponding button.

In some use cases you might not have the actual value of count variable, for example in callbacks.
In this case you can use another form of setCount function that has a previous value of the variable:

<button onClick={() => setCount(prev => prev + 1)}>
    Click Me
</button>

In the useState hook you can store any types: primitive variables, arrays, objects. Let's have a look on how to store objects in the component's state:

export const PersonData = () => {
    const [person, setPerson] = useState<Person>({ name: '', age: 0 });

    const savePerson = () => {
        setPerson({ name: "Anton", age: 20 });
    };

    return (
        <div>
            <div>{person.name} ({person.age} years)</div>
            <button onClick={() => savePerson()}>
                Set person data
            </button>
        </div>
    );
};

useEffect hook

useEffect hook allows to hook into component's life cycle and add side effects such as API calls, calculations, manual DOM updates, etc.

import { useEffect, useState } from "react";

export const CoffeeCounter = () => {
    const [count, setCount] = useState(0);

    useEffect(() => {
        console.log("Page loaded");
    }, []);

    useEffect(() => {
        console.log("Count updated: ", count);
    }, [count]);

    return (
        // Rendering code remains the same
    );
}

useEffect hooks has 2 parameters: a function that performs a side effect and an array of tracked variables.
When a variable value changes - a useEffect hook is triggered. The first hook has an empty braces [ ] without parameters, it means that this effect will be called only once after a component is loaded.
This is a perfect place to load some data from the API when page is loaded.
Second hook is called only when count variable is changed. In the CoffeeCounter component logs a message to console when a page is loaded and after clicking any button that changes the count value.

useReducer hook

useReducer is helpful when you need to perform complex state updates.
Let's rewrite our CoffeeCounter component and replace useState hook with useReducer:

import {useReducer} from "react";

type CounterState = {
  count: number  
};

type CounterAction = {
    type: string
};

function reducer(state: CounterState, action: CounterAction) {
    switch (action.type) {
        case 'increment':
            return { count: state.count + 1 };
        case 'decrement':
            return { count: state.count - 1 };
        default:
            throw new Error();
    }
}

export const ReducerCounter = () => {
    const [state, dispatch] = useReducer(reducer, { count: 0 });

    return (
        <div>
            <p>You have {state.count} coffee(s)</p>
            <button onClick={() => dispatch({ type: 'increment' })}>
                + 1 coffee
            </button>
            <button onClick={() => dispatch({ type: 'decrement' })}>
                - 1 coffee
            </button>
        </div>
    );
}

Instead of just calling a set function that sets a new value, a dispatch method from useReducer is called that can perform a complex state manipulations.

Every state update using a dispatch function requires a name of the action type, in our example we have 2 actions: increment and decrement.
After dispatch function is executed, a reducer function comes into play and performs state updates depending on a given action.

useCallback and useMemo

useCallback and useMemo help in optimizing performance by memoizing functions and values respectively, avoiding unnecessary re-renders.

import React, {useCallback, useMemo, useState} from "react";

type ExpensiveComponentProps = {
    compute: (n: number) => number;
    count: number;
}

const ExpensiveComponent: React.FC<ExpensiveComponentProps> = ({ compute, count }) => {
    const result = useMemo(() => compute(count), [compute, count]);
    return <div>Result: {result}</div>;
};

const ParentComponent: React.FC = () => {
    const [count, setCount] = useState<number>(0);

    const increment = useCallback(() => {
        setCount(c => c + 1);
    }, []);

    return (
        <div>
            <ExpensiveComponent compute={(n: number) => n * 2} count={count} />
            <button onClick={increment}>Increment</button>
        </div>
    );
};

export { ExpensiveComponent, ParentComponent };

ExpensiveComponent accepts two props: compute function and a count number.
The component uses the useMemo React hook to memorize the result of calling the compute() function with the count argument.
This result will only be re-calculated if the compute function or the count prop changes.

useCallback is used to prevent unnecessary re-renders of components.
Without useCallback, function props passed down to child components can cause them to re-render whenever the parent component renders, even if the passed functions didn't change logically.
This is because a new function is created on each render and JavaScript can't perform function equality checks.

In this example, the count state variable in the ParentComponent is incremented by 1 each time the button is clicked.
When the ExpensiveComponent receives a prop function compute - it doubles the input. So each time Expensive component receives a counter variable - it doubles the result.
On a screen you will see the following results: 0, 2, 4, 6, 8 (doubled from 0, 1, 2, 3, 4 respectively) and so on.

Summary

React Hooks is a modern way you build components in React, offering a more intuitive and functional approach to state management and side effects.
By mastering Hooks, you can simplify your components, make your code more readable and maintainable. In this blog post we explored the most common React Hooks.

  • The useState hook allows data to be saved and updated within functional components that persist through multiple component renders.
  • The useEffect hook adds side effects such as API calls and calculations, to the component's lifecycle.
  • The useReducer hook provides a more flexible method to manage complex state logic in a component, acting similar to Redux’s reducer for local state management.
  • The useCallback hook memorizes a callback function and only changes when its dependencies do, hence preventing unnecessary re-renders in child components expecting this callback as a prop.
  • The useMemo hook is used to memorize expensive computations and only recompute them when necessary, optimizing performance by avoiding unnecessary calculations.

Hope you find this blog post useful. Happy coding!

Originally published at https://antondevtips.com.

After reading the post consider the following:

  • Subscribe to receive newsletters with the latest blog posts
  • Download the source code for this post from my github (available for my sponsors on BuyMeACoffee and Patreon)

If you like my content —  consider supporting me

Unlock exclusive access to the source code from the blog posts by joining my Patreon and Buy Me A Coffee communities!

...



📌 Mastering React Hooks: A Comprehensive Guide to Functional Components


📈 82.42 Punkte

📌 Mastering React Hooks: A Comprehensive Guide for Frontend Developers


📈 55.06 Punkte

📌 Mastering Higher-Order Components in React JS: A Comprehensive Guide 🚀


📈 50.09 Punkte

📌 Mastering React Server Components and Server Actions: A Comprehensive Guide


📈 50.09 Punkte

📌 Converting React Class Components to Functional Components: A Checklist and Example


📈 47.5 Punkte

📌 🔄 Class Components vs Functional Components: A Lifecycle Journey in React 🔄


📈 47.5 Punkte

📌 Embracing Modern React: Transitioning from Class Components to Functional Components


📈 47.5 Punkte

📌 Embracing Modern React: Transitioning from Class Components to Functional Components


📈 47.5 Punkte

📌 React.memo - Optimize React Functional Components


📈 44.54 Punkte

📌 React Hooks : A Comprehensive Beginner’s Guide


📈 42.87 Punkte

📌 Understanding React Hooks: A Comprehensive Guide


📈 42.87 Punkte

📌 React Hooks – How to Use the useState & useEffect Hooks in Your Project


📈 41.62 Punkte

📌 React Hooks – How to Use the useState & useEffect Hooks in Your Project


📈 41.62 Punkte

📌 Bloodline of Hooks: Custom Hooks in React for Advanced Devs


📈 41.62 Punkte

📌 Typescript for React Components (or How To Write Components in React The Right Way)


📈 40.27 Punkte

📌 Implementing Higher Order Components (HOC) for Functional Components in ReactJS


📈 38.91 Punkte

📌 Mastering React State Management with useReducer: A Comprehensive Guide


📈 38.54 Punkte

📌 Mastering API Calls in React with Axios: A Comprehensive Guide


📈 38.54 Punkte

📌 React Server Components: A comprehensive guide


📈 37.9 Punkte

📌 This Week In React #139: React.dev, Remix, Server Components, Error Boundary, Wakuwork, React-Native, Bottom Sheet...


📈 37.32 Punkte

📌 This Week In React #146: Concurrency, Server Components, Next.js, React-Query, Remix, Expo Router, Skia, React-Native...


📈 37.32 Punkte

📌 Mastering React Hooks with Typescript


📈 37.29 Punkte

📌 Mastering Component Lifecycles: A Deep Dive into Angular and React Hooks


📈 37.29 Punkte

📌 Mastering React Hooks: Tips, Tricks, and Best Practices


📈 37.29 Punkte

📌 Working with React function components, hooks and state


📈 36.65 Punkte

📌 All About Custom Hooks: Supercharge Your React Components


📈 36.65 Punkte

📌 Getting Weird: "Hooks" in React Class Components


📈 36.65 Punkte

📌 Using Custom Hooks in multiple React Components.


📈 36.65 Punkte

📌 React - Functional Component V/S Class Components


📈 35.95 Punkte

📌 React's new killer documentation focused only on functional components


📈 35.95 Punkte

📌 Streamlining Constructors in Functional React Components


📈 35.95 Punkte

📌 Optimizing Functional React Components


📈 35.95 Punkte

📌 React Memo Mayhem: Optimizing Functional Components


📈 35.95 Punkte

📌 Mastering React Router: The Ultimate Guide to Navigation and Routing in React Apps!


📈 35.83 Punkte

📌 Elevating Your React A Comprehensive Guide to Using React-Select


📈 34.95 Punkte











matomo