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全家桶实现一个完整应 ...
随机推荐
- python day27--网络编程
一‘.网络基础 1.什么是IP IP地址是指互联网协议地址(英语:Internet Protocol Address,又译为网际协议地址),是IP Address的缩写.IP地址是IP协议提供的一种统 ...
- 自动化测试-11.selenium的下拉框处理类Select
前言 最近由于工作原因,更新慢了一点,今天终于抽出一点时间给大家继续更新selenium系列,学习的脚本不能停止,希望小伙伴能多多支持. 本篇以百度设置下拉选项框为案例,详细介绍select下拉框相关 ...
- windows 重装系统
转载:https://blog.csdn.net/qq_41137650/article/details/80035921 老毛桃 大白菜都可以 电脑系统安装步骤我选的是用U盘做的系统 如果本计系 ...
- opencv读取摄像头实时流代码
opencv读取摄像头实时流代码: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; ...
- Nmap使用指南
一.目标指定 1.CIDR标志位 192.168.1.0/24 2.指定范围 192.168.1.1-255 192.168.1-255.1(任意位置) 3.IPv6地址只能用规范的IPv6地址或主机 ...
- 怎样使用PL/SQL在不安装oracle 客户端的情况下使用oracle数据库
在网上查了好多这方面的例子,但是似乎说的都不准确,在咨询朋友后终于实现了本机不安装oracle 的情况下,在windows系统上实现连接服务器上的数据库,现在贴出来与大家共享. 首先,我们需要一个PL ...
- <Numerical Analysis>(by Timothy Sauer) Notes
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...
- 记录一下我在lubuntu里面用到的工具
文本编辑:Atom 文本对比:Beyond Compare 数据库工具:dbeaver Git工具:GitKraKen SVN工具:RapidSVN 网页编程工具:WebStorm 另外,14.04版 ...
- Zookeeper 在Windows下的安装过程及测试
安装jdk 安装Zookeeper. 在官网http://zookeeper.apache.org/下载zookeeper.我下载的是zookeeper-3.4.6版本. 解压zookeeper-3. ...
- ubuntu下的Nessus插件更新
00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...