Lädt...

🔧 Hooks behind the scene 4, UseReducer


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hey coders!!!

In this new post of react, we will check other useful hook...

UseReducer.-

👉🏻 If you find yourself keeping track of multiple pieces of state that rely on complex logic.

  • Manage complex state in react apps
  • Works like a state management (Redux, etc).

The basics:

👉🏻 useReducer(reducer, initialState)

The reducer function contains your custom state logic and the initialState can be a simple value but generally will contain an object.

The useReducer Hook returns the current state and a dispatch method.

👉🏻 As always the best way to learn is with some code.

The result of this code will be:

Image description

We can see the popular count with two buttons, increase and decrease.

1 . Let's begin with a initial state and the reducer function.

const initialState = {count: 0};
const reducer = (state, action) => {
    switch (action.type) {
        case 'increase':
            return { count: state.count + 1 }

        case 'decrease':
            return { count: state.count - 1 }

        default:
            return state;
    }

}

This portion of code will help in the main app

const [state, dispatch] = useReducer(reducer, initialState);

2 . Create the increaseCount and decreaseCount functions with dispatch:

const increaseCount = () => {
   dispatch({ type: 'increase' });
}

const decreaseCount = () => {
   dispatch({ type: 'decrease' });
}

3 . Finally the return part:

return (
    <>
      <h2>Count: {state.count}</h2>
      <button onClick={increaseCount}>Increase</button>
      <button onClick={decreaseCount}>Decrease</button>
    </>
  );

We are showing two buttons and after clicked the increaseCount and decreaseCount will be executed.

For task we can add some improvements like create an object for 'increase' and 'decrease' strings.

Here you have the full version of code:

import { useReducer } from "react";

const ACTION = {
  INCREASE: "increase",
  DECREASE: "decrease",
};

const initialState = { count: 0 };
const reducer = (state, action) => {
  switch (action.type) {
    case ACTION.INCREASE:
      return { count: state.count + 1 };

    case ACTION.DECREASE:
      return { count: state.count - 1 };

    default:
      return state;
  }
};

export default function App() {
  const [state, dispatch] = useReducer(reducer, initialState);
  const increaseCount = () => {
    dispatch({ type: ACTION.INCREASE });
  };

  const decreaseCount = () => {
    dispatch({ type: ACTION.DECREASE });
  };

  return (
    <>
      <h2>Count: {state.count}</h2>
      <button onClick={increaseCount}>Increase</button>
      <button onClick={decreaseCount}>Decrease</button>
    </>
  );
}

Conclusion:

  • We will use useReducer to manage complex states in react applications.

  • For simple applications we can use useContext or other ways to manage the state.

Let me know if this post is useful for you and also if you want to check in the same the rest of hooks missing.

Have a great day for coding :D

...

🔧 Hooks behind the scene 4, UseReducer


📈 56.51 Punkte
🔧 Programmierung

📰 Netflix Uses AI in Its New Codec To Compress Video Scene By Scene


📈 32.47 Punkte
📰 IT Security Nachrichten

🔧 🧬Hooks de React: useReducer


📈 32.17 Punkte
🔧 Programmierung

🔧 Simplifying React Hooks: useReducer 💯


📈 32.17 Punkte
🔧 Programmierung

🔧 useReducer: React Hooks


📈 32.17 Punkte
🔧 Programmierung

🔧 Mastering State Management in React: A Guide to useContext and useReducer Hooks


📈 32.17 Punkte
🔧 Programmierung

🔧 Os 10 React Hooks Mais Úteis: 05 - useReducer


📈 32.17 Punkte
🔧 Programmierung

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 28.61 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 28.61 Punkte
🕵️ Reverse Engineering

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 28.61 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 28.61 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 28.61 Punkte
🔧 Programmierung

🔧 React Hooks – How to Use the useState &amp; useEffect Hooks in Your Project


📈 24.95 Punkte
🔧 Programmierung

🔧 React Hooks – How to Use the useState &amp; useEffect Hooks in Your Project


📈 24.95 Punkte
🔧 Programmierung

🔧 Flutter Hooks Tutorial: Flutter Animation Using Hooks (useEffect and useAnimationController)


📈 24.95 Punkte
🔧 Programmierung

🔧 Advanced React Hooks: Custom Hooks and Performance Optimization


📈 24.95 Punkte
🔧 Programmierung

🔧 Advanced React Hooks: Custom Hooks and Performance Optimization


📈 24.95 Punkte
🔧 Programmierung

🔧 The Essential Rules of Hooks in React: How to Use Hooks Properly


📈 24.95 Punkte
🔧 Programmierung

🔧 🪝Mastering React Hooks Transform Your React Development with Hooks🚀


📈 24.95 Punkte
🔧 Programmierung

🔧 Deep Dive into React 🚀🚀Hooks: useState, useEffect, and Custom Hooks 🔍


📈 24.95 Punkte
🔧 Programmierung

🔧 Deep Dive into React Hooks: useState, useEffect, and Custom Hooks


📈 24.95 Punkte
🔧 Programmierung

🔧 Learn React Hooks – Common Hooks Explained with Code Examples


📈 24.95 Punkte
🔧 Programmierung

🔧 React Hooks - Top7 Most Commonly Used React Hooks for Efficient Component Management


📈 24.95 Punkte
🔧 Programmierung

🔧 scriptkavi/hooks: Customizable and Open Source React Hooks


📈 24.95 Punkte
🔧 Programmierung

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


📈 24.95 Punkte
🔧 Programmierung

🔧 How GPT Works Behind The Scene


📈 24.34 Punkte
🔧 Programmierung

🔧 Jakarta Servlets: Behind the Scene of Deployment and container Lifecycle


📈 24.34 Punkte
🔧 Programmierung

🔧 How PHP Works - Behind The Scene


📈 24.34 Punkte
🔧 Programmierung

🔧 HOW CSS WORKS BEHIND THE SCENE


📈 24.34 Punkte
🔧 Programmierung

🔧 Behind The Scene of Under Water Game Development Game Environment Development


📈 24.34 Punkte
🔧 Programmierung

matomo