React.js framework Redux basic case detailed explanation

React.js framework Redux basic case detailed explanation

react.js framework Redux

https://github.com/reactjs/redux

Install:

npm install redux react-redux
#Based on react, we have already installed it before

Redux reference documentation:
http://redux.js.org/

Redux Core Concept: Store

We can simply understand it as being used to store State of each component or the independent state you define yourself, and to perform unified operations such as reading, updating, and monitoring the state.
http://redux.js.org/docs/basics/Store.html

這里寫圖片描述

Reduce

The official tells us that the basic use of redux is as follows:

import {createStore} from "redux";
import todoApp from "./reducers";
let store = createStore(todoApp);

The parameter passed into createStore() is a function Function.
The concept in redux is called: Reduce
The basic form of Reduce is:

function myFun(state,action){
  // ...
}

Of course, we can also use the arrow function form of esmascript 2015 to define it.

Practical Exercise

1. Let's define a Reduce first

InfoReduce.js:

//Test data let info = {
    title:"Test title",
    clicknum:0
};

// Export the data through the parameter hull default (state = info, action)=>{


    return state; //The returned value is the test data}

2. Reduce is ready, let's start using Redux

import InfoReduce from "./../redux/InfoReduce";

import {createStore} from "redux";
let store = createStore(InfoReduce);

3. The very important concept store in Redux has been created. Let’s see how to use it in the component?

// Define a component called InfoDetail class InfoDetail extends React.Component{
    //Constructor(props) {
        super(props);
        // Initial state this.state = {
            infoData:store.getState() //Get data through the store object method};
      }


        render(){
            return <div>
                <h2>News title: {this.state.infoData.title}</h2>
                <span>Number of clicks: {this.state.infoData.clicknum}</span>
                <p><button>Modify click volume</button></p>
            </div>
        }
}

Get data through store.getState() .

這里寫圖片描述

At this point we basically understand: Reducers is a specified function that generates a new state and passes it to the Store; our components obtain the state through the Store to update the component data.

Understanding actions

The official documentation on action :
http://redux.js.org/docs/basics/Actions.html

In fact, through the word action we can guess that it is an operation used to handle business.

Where is action in our previous code?
When we define the Reducer function, the second parameter is:

export default (state, action)=>{}

1. Since action is an operation, it means that we need it in the event handling function on the component

<button onClick={this.addClick.bind(this)}>Modify click amount</button>

Bind a click event function addClick to the button

2. Let’s take a look at what’s going on in the addClick function.

        addClick(){
            //Modify state
            store.dispatch({
                type:"INFO_CLICK"
            })

            this.setState({ //Update state
                infoData:store.getState()
            })
        }

this.setState() We have learned before that this is used to update the state;
store.dispatch() is something in our Redux. This method actually dispatches action .
Distinguish by type.

3. According to our needs, the business logic that our action needs to handle is to increase clicknum

//Test data let info = {
    title:"Test title",
    clicknum:0
};

// Export the data through the parameter hull default (state = info, action)=>{

    if (action.type == "INFO_CLICK"){
        let oldNum = state.clicknum;
        oldNum++;

        // Return new data return Object.assign({},state,{clicknum:oldNum});
    }

    return state; //The returned value is the test data}

In our Reducer function, we use action.type to judge and then handle business logic.

At this point, we may be confused, why is Redux still troublesome? Yes, it is generally used in projects with complex business logic.

這里寫圖片描述

This is the end of this article about the detailed explanation of the basic case of Redux in the react.js framework. For more relevant basic content of Redux in the react.js framework, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Javascript Basics: Detailed Explanation of Operators and Flow Control
  • Detailed explanation of basic syntax and data types of JavaScript
  • JavaScript Basics Series: Functions and Methods
  • Detailed explanation of the basics of JSP EL expressions
  • Java Basics - FastJson Detailed Explanation
  • Detailed explanation of Javascript basics

<<:  How to uninstall MySQL 5.7 on CentOS7

>>:  Solution to Apache cross-domain resource access error

Recommend

Vue3.0 implements the magnifying glass effect case study

The effect to be achieved is: fixed zoom in twice...

Ideas and practice of multi-language solution for Vue.js front-end project

Table of contents 1. What content usually needs t...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

How to use html table (to show the visual effect of web page)

We know that when using HTML on NetEase Blog, we ...

Deep understanding of line-height and vertical-align

Several concepts Line box: A box that wraps an in...

Several ways to encapsulate breadcrumb function components in Vue3

Table of contents Preface 1. Why do we need bread...

MySql sharing of null function usage

Functions about null in MySql IFNULL ISNULL NULLI...

Shell script to monitor MySQL master-slave status

Share a Shell script under Linux to monitor the m...

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

Understanding Nginx Current Limitation in One Article (Simple Implementation)

Nginx is now one of the most popular load balance...

Implementation of grayscale release with Nginx and Lua

Install memcached yum install -y memcached #Start...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Javascript Basics: Detailed Explanation of Operators and Flow Control

Table of contents 1. Operator 1.1 Arithmetic oper...

MySQL data aggregation and grouping

We often need to summarize data without actually ...

Detailed installation tutorial of zabbix 4.04 (based on CentOS 7.6)

1. Preparation before installation: 1.1 Install J...