<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: edem-cyber</title>
    <description>The latest articles on DEV Community by edem-cyber (@edemcyber).</description>
    <link>https://dev.to/edemcyber</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F795899%2Fdba466a4-d41a-4852-8a75-38c5c14541a0.jpg</url>
      <title>DEV Community: edem-cyber</title>
      <link>https://dev.to/edemcyber</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edemcyber"/>
    <language>en</language>
    <item>
      <title>Set up Redux-Toolkit in 5 steps</title>
      <dc:creator>edem-cyber</dc:creator>
      <pubDate>Thu, 20 Apr 2023 23:21:45 +0000</pubDate>
      <link>https://dev.to/edemcyber/set-up-redux-toolkit-in-5-steps-b9a</link>
      <guid>https://dev.to/edemcyber/set-up-redux-toolkit-in-5-steps-b9a</guid>
      <description>&lt;p&gt;Before you begin, understand that you will understand how to implement and use redux-toolkit by the end of this tutorial. This tutorial is a step by step structured way to understand implementing redux-toolkit. &lt;/p&gt;

&lt;p&gt;You can always refer back to this when you need. The key to understanding redux at the core is actually implementing it a lot in projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: This does not involve the use of asynchronous functions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definitions:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Refer to these definitions whenever needed. Make sure you get what they mean. If you don’t, try giving it in an LLM like ChatGPT to explain it to you.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slice: A slice is a concept introduced by Redux Toolkit that helps you organize your Redux store by grouping together the related reducer logic into a single file. It includes reducer functions, action creators, and selectors that work together to manage a specific part of your application state.&lt;/li&gt;
&lt;li&gt;Reducer: A reducer is a function that takes the current state and an action as arguments, and returns a new state based on the action that was dispatched.&lt;/li&gt;
&lt;li&gt;Action: An action is an object that represents an intention to change the state. It contains a type field that describes the action and a payload field that contains data relevant to the action.&lt;/li&gt;
&lt;li&gt;Selector: A selector is a function that takes the current state as an argument and returns a specific piece of data from that state. Selectors can help simplify your code by abstracting away the details of how the state is structured.&lt;/li&gt;
&lt;li&gt;Middleware: Middleware is a function that sits between the dispatching of an action and the processing of that action by a reducer. It can be used for tasks like logging, error handling, or async operations.&lt;/li&gt;
&lt;li&gt;Thunks: Thunks are a type of middleware that allows you to write async logic that interacts with the store. They are used to dispatch actions that require async processing, such as fetching data from an API.&lt;/li&gt;
&lt;li&gt;Store: The store is the single source of truth for your application's state. It holds the current state tree of your application, and allows you to dispatch actions to update that state.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;set up store
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit';

export default configureStore({
  reducer: {},
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;include store in app.js
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Provider } from 'react-redux';
import store from './app/store';


// ...
  &amp;lt;Provider store={store}&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;create a slice.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from '@reduxjs/toolkit';

export const todoSlice = createSlice({
  name: 'todos',
  initialState: [],
  reducers: {
    addTodo: (state, action) =&amp;gt; {
      const todo = {
        id: uuid(),
        text: action.payload,
      };

      state.push(todo);
  },
});

// this is for dispatch
export const { addTodo } = todoSlice.actions;

// this is for configureStore
export default todoSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;add slice to store
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit';
import todoReducer from '../features/todo/todoSlice';

export default configureStore({
  reducer: {
    todos: todoReducer,
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;access and retrieve using useSelector and useDispatch
&lt;strong&gt;useSelector&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { useSelector } from 'react-redux';

export default function Todos() {
  const todos = useSelector((state) =&amp;gt; state.todos);
  // todos comes from the reducer attribute name
  // in configureStore

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;ul&amp;gt;
        {todos.map((todo) =&amp;gt; (
          &amp;lt;li key={todo.id}&amp;gt;
            &amp;lt;span&amp;gt;{todo.text}&amp;lt;/span&amp;gt;
          &amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;useDispatch&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { addTodo } from '../features/todos/todosSlice';

export default function AddTodo() {
  const [text, setText] = useState('');
  // initial the dispatch here
  const dispatch = useDispatch();

  const addTodoHandler = (event) =&amp;gt; {
    event.preventDefault();
    // update the state here using addTodo action
    // action only receive one parameter, which is payload
    dispatch(addTodo(text));
    setText('');
  };

  return (
    &amp;lt;form onSubmit={addTodoHandler}&amp;gt;
      &amp;lt;input
        type='text'
        value={text}
        onChange={(e) =&amp;gt; setText(e.target.value)}
      /&amp;gt;
      &amp;lt;button&amp;gt;Add todo&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations!, you are have joined one of the chosen.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
