AlexisTercero55 / react-bootstrap-vite-redux

Web App templete for using React, Redux and Bootstrap all powered by Vite.
https://alexistercero55.github.io/react-bootstrap-vite-redux/
Mozilla Public License 2.0
1 stars 0 forks source link

Redux set up #2

Closed AlexisTercero55 closed 1 year ago

AlexisTercero55 commented 1 year ago
AlexisTercero55 commented 1 year ago

Redux set up

AlexisTercero55 commented 1 year ago

Redux set up based on Installation guide

"dependencies": {
    "@reduxjs/toolkit": "^1.9.2",
    "bootstrap": "^5.2.3",
    "react": "^18.2.0",
    "react-bootstrap": "^2.7.2",
    "react-dom": "^18.2.0",
    "react-redux": "^8.0.5",
    "redux": "^4.2.1"
  },
  "devDependencies": {
    "@types/react": "^18.0.27",
    "@types/react-dom": "^18.0.10",
    "@vitejs/plugin-react": "^3.1.0",
    "vite": "^4.1.0"
  }
AlexisTercero55 commented 1 year ago

Reading

Getting Started with Redux

AlexisTercero55 commented 1 year ago

How Redux works

The whole global state of your app is stored in an object tree inside a single store.

The only way to change the state tree is to create an action, an object describing what happened, and dispatch it to the store.

To specify how state gets updated in response to an action, you write pure reducer functions that calculate a new state based on the old state and the action.

import { createStore } from 'redux'

/**
 * Reducer - a function that takes a current state value and an
 * action object describing "what happened", and returns a new state value.
 * A reducer's function signature is: (state, action) => newState
 *
 * The Redux state should contain only plain JS objects, arrays, and primitives.
 * The root state value is usually an object. It's important that you should
 * not mutate the state object, but return a new object if the state changes.
 */
function anyReducer(state, action) => newState;

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(anyReducer);

Decentralized state Redux management

In a typical Redux app, there is just a single store with a single root reducing function.

As your app grows, you split the root reducer into smaller reducers independently operating on the different parts of the state tree.

This is exactly like how there is just one root component in a React app, but it is composed out of many small components.

AlexisTercero55 commented 1 year ago

Counter app setup

Based on Redux Toolkit React Counter App

AlexisTercero55 commented 1 year ago

Follow these steps for setting up Counter app.

AlexisTercero55 commented 1 year ago

Redux Toolkit implementation for Counter App

Store creation

https://github.com/AlexisTercero55/react-bootstrap-vite/blob/ede5ffaed1fb75065ae7f25535aad5469ad42a01/src/store/store.js#L23

Slice creation

https://github.com/AlexisTercero55/react-bootstrap-vite/blob/ede5ffaed1fb75065ae7f25535aad5469ad42a01/src/features/counterSlice.js#L23

AlexisTercero55 commented 1 year ago

Review test requirements

<img src="https://user-images.githubusercontent.com/87354316/219885248-9ac7a70a-c71d-4b2a-bccc-e20eb3b0dded.svg" alt="Redux" height="400"/>

AlexisTercero55 commented 1 year ago

Redux basic testing