引入 Redux 的目的, 状态管理! React-Redux 就是完成一些粘合剂的作用。
简而化之的理解就是将数据放在store 中维护, 操作逻辑放在reducer中去写。
更功利的表达就是:  就是引入以后, 写控件的时候 根据props 去展示数据,操作也在props去引用。 各司其职。

Redux 使用到:createStore, dispatch
代码参考:

import React from "react";
import {connect} from "react-redux"; class App extends React.Component{
constructor(){
super();
} render(){
return <div>
<h1>{this.props.v}</h1>
<button onClick={()=>{
this.props.add()
}}>按我加1</button>
</div>
}
} export default connect(
(state) => ({
v : state.v
}),
(dispatch) => ({
add(){
dispatch({"type" : "ADD"})
}
})
)(App);

相应配置。 npm安装React, React-redux, redux-logger(每次调用dispatch 之前后都可以看状态, 方便调试)

//展示界面。 redux的createStore

import React from "react";
import ReactDOM from "react-dom";
import {createStore , applyMiddleware} from "redux";
import {Provider} from "react-redux";
import reduxLogger from "redux-logger"; import App from "./App.js";
import reducer from "../reducers"; // 创建store,项目只有唯一的一个store,全局数据。applyMiddleware表示Logger插件。
const store = createStore(reducer, applyMiddleware(reduxLogger));
console.log(store.getState()); ReactDOM.render(
<Provider store={store}>
<App></App>
</Provider>
,
document.getElementById("app")
)
//控件App 只管渲染界面,connect 使用。
import React from "react";
import {connect} from "react-redux"; export class App extends React.Component{
constructor(){
super();
} render(){
return <div>
<h1>{this.props.v}</h1>
<button onClick={()=>{
this.props.add()
}}>按我加1</button>
</div>
}
} export default connect(
(state) => ({
v : state.v
}),
(dispatch) => ({
add(){
dispatch({"type" : "ADD"})
}
})
)(App);
//reducers/index.js 操作部分
export default (state = {"v" : 100} , action) => {
if(action.type == "ADD"){
return {
"v" : state.v + 1
}
} else if (action.type == "MINUS") {
return {
"v": state.v - 1
}
}
return state;
}

进一步 分离actions 。 如上。

export default connect(
(state) => ({
v : state.v
}),
(dispatch) => ({
add(){
dispatch({"type" : "ADD"})
}
})
)(App); 变更为:
export default connect(
(state) => ({
v : state.v
}),
(dispatch) => {
return {
actions:bindActionCreators(actions, dispatch)
}
}
)(App);
//需要引入,增加actions 文件:
import {bindActionCreators} from "redux"
import * as actions from "./actions.js" //action.js 文件:
export const add = ()=> {
//console.log("未点击时,已经执行函数绑定操作")
return {"type":"ADD"}
}
export const minus = ()=> {return {"type":"MINUS"}} export const attachNumber = (number) =>{
return {"type":"ATTACHNUMBER","number":number}
}

如果需要传参操作: 
需要

//reducers/index.js 对传递参数的使用
else if(action.type == "ATTACHNUMBER") {
return {
...state,
"v": state.v + action.number
}
} //App.js 中对应取值。
attachNumber(){
let num = Number(this.refs.numberText.value);
this.props.actions.attachNumber(num);
} render(){
return <div>
<h1>{this.props.v}</h1>
<button onClick={()=>{
this.props.actions.add()
}}>按我加1</button>
<br />
<input type="text" ref="numberText" />
<input type="button" value="增加特定数" onClick={
(this.attachNumber).bind(this)}/> </div>
}

参考:

阮一峰: http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html
https://www.jianshu.com/p/5b3f874cd7a9
中介者模式: https://blog.csdn.net/qq3965470/article/details/52304399

React 环境增加Redux ,React-Redux的更多相关文章

  1. React环境配置(第一个React项目)

    使用Webpack构建React项目 1. 使用NPM配置React环境 NPM及React安装自行百度 首先创建一个文件夹,the_first_React 进入到创建好的目录,npm init,然后 ...

  2. Flux --> Redux --> Redux React 入门

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  3. Flux --> Redux --> Redux React 基础实例教程

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  4. Flux --> Redux --> Redux React 入门 基础实例使用

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  5. react 创建项目 sass router redux

    ​ 创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...

  6. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  7. RxJS + Redux + React = Amazing!(译二)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...

  8. [Redux] React Todo List Example (Toggling a Todo)

    /** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...

  9. [Redux] React Todo List Example (Adding a Todo)

    Learn how to create a React todo list application using the reducers we wrote before. /** * A reduce ...

随机推荐

  1. golang struct

    ex1 /* https://golangbot.com/structs/ struct 结构 结构就是一组字段. */ package main import "fmt" // ...

  2. sort_gff.py

    import sys infile = sys.argv[1]outfile = sys.argv[2] gff_list = []fh = open(infile)for x in fh:    i ...

  3. Python全栈之路----目录

    Module1 Python基本语法 Python全栈之路----编程基本情况介绍 Python全栈之路----常用数据类型--集合 Module2 数据类型.字符编码.文件操作 Python全栈之路 ...

  4. SQLI DUMB SERIES-18

    (1)对username和password无论怎么输入,都没有回显,再看题目,POST - Header Injection - Uagent field - Error based (基于错误的用户 ...

  5. biaffineparser

    代码: 一.pytorch,可以直接跑 https://github.com/chantera/biaffineparser python parser.py train --out model -- ...

  6. 使用CUPS打印服务

    目录 1. 测试环境 2 2. CUPS介绍 3 2.1 CUPS的配置文件 3 2.1.1 cupsd.conf 3 2.1.2 cups-files.conf 3 2.1.3 printcap 3 ...

  7. 真tm郁闷

    昨天这时还是信心满满,今天这时就已经彻底颓了. 感觉这次是最接近的了,4个题目都做出来了,怎么还会fail,那边也不说为什么,到底是哪里不足,尽说些没用的. 前后都当了4次炮灰了,以后也不会再冲动了, ...

  8. JS制作图片切换

    <!DOCTYPE html> <html> <head> <title>纯JS制作简单的图片切换</title> <meta cha ...

  9. 浅谈C#语言中的各种数据类型,与数据类型之间的转换

    什么是数据类型? 数据类型,百度百科是这样解释的:数据类型在数据结构中的定义是一个值的集合以及定义在这个值集上的一组操作.这样的解释对于一个初学者来说未必太过于深奥. 简单点说,数据类型就是不同长度的 ...

  10. [Ms SQL] 基本創建、修改與刪除

    ##創建 table student, 內涵 id ,name ,tel三種columne,設定id為primary key create table student ( id int primary ...