React 环境增加Redux ,React-Redux
引入 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的更多相关文章
- React环境配置(第一个React项目)
使用Webpack构建React项目 1. 使用NPM配置React环境 NPM及React安装自行百度 首先创建一个文件夹,the_first_React 进入到创建好的目录,npm init,然后 ...
- Flux --> Redux --> Redux React 入门
本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...
- Flux --> Redux --> Redux React 基础实例教程
本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...
- Flux --> Redux --> Redux React 入门 基础实例使用
本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...
- react 创建项目 sass router redux
创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...
- RxJS + Redux + React = Amazing!(译一)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...
- RxJS + Redux + React = Amazing!(译二)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...
- [Redux] React Todo List Example (Toggling a Todo)
/** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...
- [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 ...
随机推荐
- Python matplotlib.pyplot
Customize the label, title, and ticks. Add Color to bubbles Add Text & Grid
- postman中常见的错误
get请求400错误,post请求405错误 (2016-08-31 17:19:27)转载▼出现错误原因,后台接收参数part使用的是List,参数的属性对不上,传参使用的类型是String,改为p ...
- Git学习笔记--命令
git init--初始化Git仓库 git add <fils>--将文件添加到暂存区,可添加多个文件,空格隔开 git commit--提交到仓库 git status--查看工作区状 ...
- yii2 rules 验证规则
yii2 框架定义的约束 public $builtInValidators = [ 'boolean' => 'yii\validators\BooleanValidator', 'capt ...
- IntelliJ常用快捷键及配置
IntelliJ常用快捷键及配置 目录: 1.常用快捷键: 2.常用配置: 1.常用快捷键: (1)psvm:创建main函数 (2)fori:for (int i = 0; i < ; i++ ...
- 前端---js02
主要内容 1.数组 2.字符串 3.Date日期对象 4.内置对象 5.定时器 6.DOM 7.伪数组 内置对象: 1 数组(列表) Array (1) 数组的创建 <script>//字 ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- 采用CAS算法 实现高性能的Disruptor 完成多线程下并发、等待、先后等操作
来源:https://blog.csdn.net/tianyaleixiaowu/article/details/79787377 拓展: https://www.jianshu.com/p/d24b ...
- annotation的理解
Annotations提供一些本来不属于程序的数据. 比如:一段代码的作者或者告诉编译器禁止一些特殊的错误.An annotation 对代码的执行没有什么影响.Annotations使用@annot ...
- 用理论告诉你 三极管和MOS管的区别在哪
在电路设计当中假设我们想要对电流中止控制,那就少不了三极管的帮助.我们俗称的三极管其全称为半导体三极管,它的主要作用就是将微小的信号中止放大.MOS管与三极管有着许多相近的地方,这就使得一些新手不断无 ...