Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ Experience the Power of Redux Toolkit

๐Ÿ  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



๐Ÿ“š Experience the Power of Redux Toolkit


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Introduction:

Redux Toolkit (RTK) is a set of utilities for Redux, a popular state management library for JavaScript applications. It provides a set of conventions and utilities that make it easier to build Redux applications by reducing the amount of boilerplate code you need to write. It structures our Redux code in a way that is easier to understand and maintain

Why Redux Toolkit?

The problem with vanilla redux is it's having a lot of boilerplate code, which most of the developers don't want to do, we have to write action creators again and again in vanilla redux. Also, it requires more libraries to make it more effective
Redux toolkit is a reincarnation of redux, it makes the developer's life so easy.

The reason for preferring the Redux toolkit:

  1. Less boilerplate code compared to vanilla redux
  2. No need to do extra setup for asynchronous operations,it has createAsyncThunk which will take care of it
  3. It's easy to understand, we can achieve the same functionality that vanilla redux provides in a very short and sweet way.

Since 2019 Redux Toolkit is the officially recommended approach to write any Redux code.

Installation:

We need to install two packages

npm install @reduxjs/toolkit react-redux

       or

yarn add @reduxjs/toolkit react-redux

For creating a new React based Project with a Redux toolkit pre-installed

npx create-react-app my-app --template redux

Setup store

In Redux, a store is an object that holds the application's state.
It provides a way to access, update, and monitor the state. To create the store we have to import the configureStore function from the Redux toolkit.

Before configuring the store we have to create a redux reducer function that will specify how the state should be updated depending upon the actions triggered. Reducer function will take two arguments current state and action, it returns a new state depending upon the action.

Step 1: Implement createSilce method and export actions and reducer.

In Redux, a slice is a piece of the application's state that is managed by a specific reducer. Redux Toolkit provides the createSlice utility for creating slices of state and the corresponding reducer and action creators. createSlice takes an object that specifies the initial state, reducer functions, and action creators for the slice.

import { createSlice } from '@reduxjs/toolkit';
const todoListSlice = createSlice({
    name: "todoList",
    initialState: [],
    reducers: {
        addTodo: (state, action) => {
          state.push(action.payload);
    },
        removeTodo: (state, action) => {
          state.filter(todo => todo.id !== action.payload);
    }
    }
});
export const { addTodo, removeTodo } = todoListSlice.actions;
export default todoListSlice.reducer;

Step 2: Add Slice Reducers to the Store

In Redux, the configureStore function is a utility function provided by the redux-toolkit package that helps you set up a Redux store for your application. It takes a single argument, an options object, and returns a configured Redux store object.

import { configureStore } from '@reduxjs/toolkit';
import { combineReducers } from 'redux';
import todoList from 'todoListSlice';

const rootReducer = combineReducers({todoList}) which is similar to
const rootReducer = combineReducers({todoList:todoList})

If we want to use different attribute name then we can write like this
const example=combineReducers({attributeName:reducerName})

const store = configureStore({ devTools: true, reducer: rootReducer });
export default store;

We have to pass a single reducer function to our store, as the app grows more complex, you'll want to split your reducing function into separate functions, each managing independent parts of the state.
The combineReducers helper function turns an object whose values are different reducing functions into a single reducing function.
for ex: const rootReducer=combineReducers({reducer1,reducer2,....,reducer-N})

The configureStore function accepts several options that allow you to customize the behavior of the store. Some common options include:

  • reducer: The root reducer of our application

  • devTools: This will allow us to inspect the state of our store

  • preloadedState: The initial state of the store.

Other than this there are many other options are vailable for configureStore. If you want more information about configureStore you can read the documentation here

Step 3: Connect our store to the app

Now we need to connect our store to the app. This will make our app access the redux store that we have created.

import { Provider } from 'react-redux';
import store from './redux/store';
.....
 <Provider store={store}>
        <App />
 </Provider>
.....

Step 4: Read and update the global state using useSelector() and useDispatch() hooks

By using useSelector and useDispatch from react-redux, we can read the state from a Redux store and dispatch any action from a component, respectively.

useSelector():

useSelector is a function that takes the current state as an argument and returns whatever data you want from it.

import { useSelector } from 'react-redux';

const list = useSelector((state) => state.todoList);

// Here todoList is the attribute name given to the reducer 


useDispatch():

If we want to modify the global state we need to use useDispatch and the action that we already created in slice.

import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { addTodo } from 'todoListSlice';

export default const MyTodoList=()=> {
  const [text, setText] = useState('');
  // We will invoke useDispatch and store it to a variable
  const dispatch = useDispatch();

  const addTodo = (e) => {
    e.preventDefault();
// Here we are updating the todoList state by making use of the action that we have created in todoList slice
    dispatch(addTodo(text));
    setText('');
  };

  return (
    <form onSubmit={addTodo}>
      <input
        type='text'
        value={text}
        onChange={(e) => setText(e.target.value)}
      />
      <button>Add todo</button>
    </form>
  );
}

If we want to remove the list from todoList we have to dispatch removeTodo action.

Handling asynchronous action in Redux toolkit

To perform asynchronous logic before sending the processed result to the reducers we have to make use of createAsyncThunk which is provided by toolkit

import { createSlice } from '@reduxjs/toolkit'
  const asynchronousSlice = createSlice({
  name: 'async',
  initialState={
   isLoading:false,
   data:[],
   isSuccess:false,
   message:""
},
  reducers: {},
  extraReducers: {},
})

export default asynchronousSlice.reducer;

Within createSlice,synchronous requests are handled by reducer object and asynchronous requests are handled by extraReducer.

import { createAsyncThunk } from '@reduxjs/toolkit'
export const getData=createAsyncThunk("async/fetch",async(arg,thunkApi)=>{
const response=await fetch('url')
return response.data

})

This will create an action with async/fetch. The action will also have three additional properties pending, fulfilled, and rejected, which can be used to track the status of the async action. Depending upon this we can show a loading screen in UI to make it more user friendly

import { createSlice } from '@reduxjs/toolkit'
  const asynchronousSlice = createSlice({
  name: 'async',
  initialState={
   isLoading:false,
   data:[],
   isSuccess:false,
   message:""
},
  reducers: {},
  extraReducers: {

 [getData.pending]: (state) => {
      state.isLoading = true
    },
 [getData.fulfilled]: (state, action) => {
      state.isLoading = false
      state.data = action.payload
      state.isSuccess=true
    },
 [getData.rejected]: (state, action) => {
      state.isLoading = false
      state.message = action.error
    }

},
})

export default asynchronousSlice.reducer;

Let's setup a component which will dispatch getData when it mounts.

import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { getData } from 'asynchronousSlice'

export default function Home() {
  const dispatch = useDispatch()
  const { data, isLoading } = useSelector((state) => state.async)

  useEffect(() => {
    dispatch(getData())
  }, [])

  if (loading) return <p>Loading...</p>

  return (
    <div>
      <h2>Details</h2>
      {data.map((post) => (
        <p key={post.id}>{post.title}</p>
      ))}
    </div>
  )
}

Conclusion and Thoughts

Redux Toolkit can be a useful tool for reducing the amount of code you have to write when working with Redux, and for helping you structure your code in a way that is easier to understand and maintain.

Thanks for reading this article, and I hope you enjoyed reading it and learned something about Redux toolkit. Please leave a comment if you have any questions for me

...



๐Ÿ“Œ Setting up Redux Persist with Redux Toolkit in React JS


๐Ÿ“ˆ 41.79 Punkte

๐Ÿ“Œ Exploring Redux Toolkit 2.0 and the Redux second generation


๐Ÿ“ˆ 41.79 Punkte

๐Ÿ“Œ Experience the Power of Redux Toolkit


๐Ÿ“ˆ 40.84 Punkte

๐Ÿ“Œ Connecting Redux Form with React Redux


๐Ÿ“ˆ 31.09 Punkte

๐Ÿ“Œ Connecting Redux Form with React Redux


๐Ÿ“ˆ 31.09 Punkte

๐Ÿ“Œ React Redux Tutorial #1 - Was ist React Redux


๐Ÿ“ˆ 31.09 Punkte

๐Ÿ“Œ React Redux Tutorial #1 - Was ist React Redux


๐Ÿ“ˆ 31.09 Punkte

๐Ÿ“Œ This Week In React #130: Next.js, callback ref, Zod, Redux, React-Hook-Form, Redux, mdxjs-rs, Tamagui, Skia, Shopify, Solid...


๐Ÿ“ˆ 31.09 Punkte

๐Ÿ“Œ Bring some power with Anker's PowerCore Redux power banks on sale from $34


๐Ÿ“ˆ 29.02 Punkte

๐Ÿ“Œ Inside the Microsoft Power Platform | Power Apps, Power Automate, Power BI and more


๐Ÿ“ˆ 26.95 Punkte

๐Ÿ“Œ A step-by-step guide on using Redux Toolkit with React


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ How to Use Redux Toolkit to Manage State in Your React Application


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ A Guide To Redux Toolkit With TypeScript


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Redux Toolkit Simplified


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Have You Met Redux Toolkit? A Friendly Companion for Simpler State Management in React ๐Ÿค—!


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ How to create your own Redux Toolkit in raw js?


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ The Best Vscode Snippets for React Typescript + Nextjs + Redux Toolkit


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Redux Toolkit State Management in React


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Add to Cart Feature in React with Redux Toolkit


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ A Beginner's Guide to React Redux Toolkit: Simplify Your State Management


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Learn React 18 with Redux Toolkit โ€“ Full Tutorial for Beginners


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Integration of Firebase Firestore With Redux Toolkit Query


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Context API vs Redux-Toolkit


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ ๐Ÿ”ฅBuild A Movie Search App Using Redux Toolkit And React Router 6๐Ÿ”ฅ


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ How to get started with Redux Toolkit!


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Redux Toolkit Query - Cache Behavior


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ Understand Redux Toolkit Query and its Applications in Web App Development


๐Ÿ“ˆ 26.24 Punkte

๐Ÿ“Œ O365-Attack-Toolkit - A Toolkit To Attack Office365


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ Firmware Analysis Toolkit - Toolkit To Emulate Firmware And Analyse It For Security Vulnerabilities


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ IoT-Implant-Toolkit - Toolkit For Implant Attack Of IoT Devices


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ Name clash with KDE MAUI toolkit by Microsoft MAUI toolkit


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ IBM/fhe-toolkit-linux: IBM Fully Homomorphic Encryption Toolkit For Linux. - GitHub


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ MSMG ToolKit 12.9 mit optimiertem Toolkit-Code und Korrekturen - Deskmodder.de


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ Announcing a new name for the UWP Community Toolkit: Windows Community Toolkit


๐Ÿ“ˆ 21.39 Punkte

๐Ÿ“Œ .NET Community Toolkit: Neue Version 8.0 bringt neues MVVM Toolkit und mehr


๐Ÿ“ˆ 21.39 Punkte











matomo