react.js framework Reduxhttps://github.com/reactjs/redux Install: npm install redux react-redux #Based on react, we have already installed it before Redux reference documentation: Redux Core Concept: Store We can simply understand it as being used to store ReduceThe 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 function myFun(state,action){ // ... } Of course, we can also use the arrow function form of esmascript 2015 to define it. Practical Exercise1. 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 // 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 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 In fact, through the word action we can guess that it is an operation used to handle business. Where 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 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() }) } 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 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:
|
<<: How to uninstall MySQL 5.7 on CentOS7
>>: Solution to Apache cross-domain resource access error
The effect to be achieved is: fixed zoom in twice...
Table of contents 1. What content usually needs t...
1. The difference between forward proxy and rever...
We know that when using HTML on NetEase Blog, we ...
Several concepts Line box: A box that wraps an in...
Table of contents Preface 1. Why do we need bread...
Functions about null in MySql IFNULL ISNULL NULLI...
Share a Shell script under Linux to monitor the m...
Zen Coding It is a text editor plugin. In a text ...
Nginx is now one of the most popular load balance...
Install memcached yum install -y memcached #Start...
Table of contents 1. Prerequisites 1.1 Supported ...
Table of contents 1. Operator 1.1 Arithmetic oper...
We often need to summarize data without actually ...
1. Preparation before installation: 1.1 Install J...