Giter Site home page Giter Site logo

katefrontend / redux-basics Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 359 KB

Redux basic information to give you an understanding of its concept and basic principles.

HTML 34.86% JavaScript 65.14%
react react-redux reactjs redux redux-basic redux-concepts redux-practice redux-store

redux-basics's Introduction

My Skills Redux Basics

Redux is a pattern and library for managing and updating application state, using events called "actions". It serves as a centralized store for state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion.

Official source https://redux.js.org/

  • React-redux

Redux can integrate with any UI framework (Angular, Vue, Vanila Js etc.), and is most frequently used with React. React-Redux is an official package that lets your React components interact with a Redux store by reading pieces of state and dispatching actions to update the store.

  • Redux Toolkit

Redux Toolkit is recommended approach for writing Redux logic. It contains packages and functions that are essential for building a Redux app. Redux Toolkit builds best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.

โšก Install

npm install redux

npm install redux react-redux

๐Ÿฆ‰ Terminology

Let's imagine that the user wants to add items to the cart.
To use Redux for this, let's analyze the following importants Redux terms:

  • STORE
  • ACTION
  • REDUCER
  • DISPATCH

ACTION - you can think of an action as an event that describes something that happened in the application.

Action does't change the state, it's just an intention to do something.

A - one of the possible user actions (add to cart)

const addToCart = () => {
    return {
        type: "ADD_TO_CART"
    }
}

B - another one of the possible user actions (remove item from cart)

const removeItem = () => {
    return {
        type: "REMOVE_ITEM"
    }
}

C - another one of the possible user actions (change the quantity of the item in the cart)

const changeNumber = () => {
    return {
        type: "CHANGE_NUMBER"
    }
}

REDUCER - you can think of a reducer as an event listener which handles events based on the received action (event) type.

A description of how our intentions to make actions will change the state. Reducer does't change the state.

The logic inside reducer functions typically follows the same series of steps:

  • Check to see if the reducer cares about this action
  • If so, make a copy of the state, update the copy with new values, and return it Otherwise, return the existing state unchanged.

Reducers can use any kind of logic inside to decide what the new state should be: if/else, switch, loops, and so on. I'll use switch logic here:

const cart = (state = 0, action) => {
    switch(action.type) {
        case "ADD_TO_CART":
        return state +1;

        case "REMOVE_ITEM":
        return state -1;

        default:
        return state;
    }
}

STORE - the current Redux application state lives in an object called the store.

import { createStore } from 'redux'

let store = createStore(cart);

DISPATCH - the only way to update the state is to call store.dispatch() and pass in an action object.

The store will run its reducer function and save the new state value inside:

store.dispatch(addToCart())
store.dispatch(addToCart())
store.dispatch(removeItem())

To check what our console shows we need to use a method called getState that returns the current state value:

let store = createStore(cart);
store.subscribe(() => console.log(store.getState()));

Work environment

The best way to work with Redux is to create the separate folders (here I wrote the logic in index.js file). See examples on the code source in the src folder and index.js file.

redux-basics's People

Contributors

katefrontend avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.