Redux使用教程
在开始之前,需要安装环境,node.js可以使用npm管理包,开发的工具webstorm可以创建相应的项目。
项目中redux是管理全局的同一个store,React-router是管理路由的,这里只使用了redux,react-router稍后介绍。
1、先创建一个简单的项目。安装对应的包redux、react-redux、redux-thunk(异步操作action时使用)

2、建立全局使用State,也就是store。
import {combineReducers} from 'redux'; //combineReducers作用可以将多个reducer合并成一个,
const counterReducer = (state = {value: 0}, action) => {
//根据dispatch触发的action类型来修改对应state
switch (action.type) {
case 'add':
state = {value: state.value + 1}
break;
case 'delete':
state = {value: state.value - 1}
break;
default:
break;
}
return state;
}
export default combineReducers({
counterReducer //这里是添加一个state值,也可以修改成 counter: counterReducer,这样在state中的key为counter来获取值
})
3、创建store
import {createStore, applyMiddleware} from 'redux';
import {createLogger} from 'redux-logger'; //日志有关
import thunk from 'redux-thunk'; //这个必须要引入到中间件中
import Init from './InitReduxCollection'; //这个是上面创建的reducer
const middleware = [thunk]; //与异步操作有关,必须要引入,不然报错
const store = createStore(Init, applyMiddleware(...middleware)); //applyMiddleware中间件,可以放入多个参数,日志中间件要放在最后
export default store
4、页面关联action和reducer
/**
* Created by Administrator on 2019/1/2.
*/ import React, {Component} from 'react';
import {connect} from 'react-redux'; //这里要注意
import PropTypes from 'prop-types' import './home-page.css'; class HomePage extends Component { static propTypes = {
value: PropTypes.number
}; addClick = () => {
console.log('add....')
let {Add} = this.props;
Add(); }
deleteClick = () => {
console.log('delete....')
this.props.Delete()
} render () {
console.log(this.props) return <div className="home">
<div>hello world !!!! </div> <div className="button" onClick={this.deleteClick}>-</div> {this.props.value} <div className="button" onClick={this.addClick}>+</div> </div>
}
} //这里是所有action的方法
const mapDispatchToProps = {
//这里先不着急创建action,先把页面和state关联,然后创建action,在将对应的action放入就可以了,可以看到第七步创建action
//Add,
//Delete
}
//这里是将所有reducer的state转成关联页面的props,可以通过props来访问
const mapStateToProps = (state) => {
console.log(state) //这里打印会看到reducer合并的所有state
return {
...state.counterReducer
}
} //将页面和全局的store关联
export default connect(mapStateToProps, mapDispatchToProps)(HomePage);
5、创建页面和store关联
import React , {Component} from 'react';
import {Provider} from 'react-redux'; //用于页面和全局store关联
import store from './page/Store'; //这个是上面第三步创建store
import HomePage from './page/Home/HomePage'; //对应的页面
export default class AppContainer extends Component {
render () {
return <Provider store={store}>
<HomePage/>
</Provider>
}
}
6、修改显示页面,然后运行看看效果页面是否可以展示,此时action是不能触发,
ReactDOM.render(<AppContainer />, document.getElementById('root'));
7、创建action,然后可以将其关联到第四部中。
export const Add = (param) => (dispatch, getState) => {
//这里参数dispatch是页面关联时,调用传入的参数,param是调用Add时传入的参数,dispatch调用时传入参数中type是必须的,其他的可以自己定义参数名,比如dispatch({type:'add', value:2}),这个dispatch触发add的reducer,action就是这里传入的参数
dispatch({
type: 'add'
});
}
export const Delete = () => (dispatch, getState) => {
dispatch({
type: 'delete'
});
}
Redux使用教程的更多相关文章
- Redux 入门教程
Redux 入门教程(三):React-Redux 的用法(53@2016.09.21) Redux 入门教程(二):中间件与异步操作(32@2016.09.20) Redux 入门教程(一):基本用 ...
- Redux 入门教程(二):中间件与异步操作
上一篇文章,介绍了 Redux 的基本做法:用户发出 Action,Reducer 函数算出新的 State,View 重新渲染. 但是,一个关键问题没有解决:异步操作怎么办?Action 发出以后, ...
- [转] Redux入门教程(快速上手)
学习前提 在我们开始以前,确保你熟悉以下知识: 函数式JavaScript 面向对象JavaScript JavaScript ES6 语法 同时,确保你的设备已经安装: NodeJS Yarn(或者 ...
- Redux 入门教程(三):React-Redux 的用法
为了方便使用,Redux 的作者封装了一个 React 专用的库 React-Redux,本文主要介绍它. 这个库是可以选用的.实际项目中,你应该权衡一下,是直接使用 Redux,还是使用 React ...
- Redux 入门教程(一):基本用法
转自http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html(仅供个人学习使用) 首先明确一点, ...
- redux使用教程详细介绍
本文介绍redux的使用 安装 cnpm install redux --save cnpm install react-redux --save cnpm install redux-devtool ...
- Redux 入门到高级教程
Redux 是 JavaScript 状态容器,提供可预测化的状态管理. (如果你需要一个 WordPress 框架,请查看 Redux Framework.) Redux 除了和 React 一起用 ...
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- 实例讲解react+react-router+redux
前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...
随机推荐
- cat语法
语法格式 cat [-AbeEnstTuv] [--help] [--version] fileName 参数说明: -n 或 --number:由 1 开始对所有输出的行数编号. -b 或 --nu ...
- 项目配置linux上, 配置文件访问不到
/** * 读入TXT文件 */public List<String> readFile(String pathName) { List<String> list = new ...
- 使用Ajax+jQuery来实现前端收到的数据在console上显示+简单的主页设计与bootstrap插件实现图片轮播
1.实现前端输入的数据在console上显示 上一篇是解决了在前端的输入信息在cygwin上显示,这次要给前台们能看见的数据,因为数据库里插入的数据少,所以写的语句翻来覆去就那几个词,emmm···当 ...
- C#中关于对out和ref的认识
1.两者都是按照地址进行传递的.2.ref关键字是的参数按照引用传递的方式进行.他的效果是控制权传递会调用方法是,可以对这个参数进行修改 要使用ref参数,那么方法的定义和调用方法都必须进行显示的 ...
- Python学习二十八周(vue.js)
一.指令 1.一个例子简单实用vue: 下载vue.js(这里实用1.0.21版本) 编写html代码: <!DOCTYPE html> <html lang="en&qu ...
- cocos2dx开发之util类&方法——字符串替换
/*将originStr字符串中的searchStr替换成replaceStr*/ std::string str_replace(std::string originStr,std::string ...
- SQLI DUMB SERIES-11
(1)检测构造方式 由此看出输入的用户名以及密码都被一对单引号所包含. 方法一: (2) 模拟真实环境,以用户的身份登录. (3)用burp抓包.开启抓包,输入用户名和密码,会自动跳到这个页面,右键, ...
- msm8909平台JEITA配置和bat-V therm表合入
8909平台的冷热充电温度点是硬件控制的,不能软件控制,目前硬件设置的是0~55度的充电区间. 软件上应该设置的是BTM comparator threshold, 70%(cold)~35%(hot ...
- python:Hamlet英文词频统计
#CalHamletV1.py def getText(): #定义函数读取文件 txt = open("hamlet.txt","r").read() txt ...
- Go语言开发Windows应用
Go语言开发Windows应用 当第一次看到Go程序在windows平台生成可执行的exe文件,就宣告了windows应用也一定是Go语言的战场.Go不是脚本语言,但却有着脚本语言的轻便简单的特性.相 ...