在开始之前,需要安装环境,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使用教程的更多相关文章

  1. Redux 入门教程

    Redux 入门教程(三):React-Redux 的用法(53@2016.09.21) Redux 入门教程(二):中间件与异步操作(32@2016.09.20) Redux 入门教程(一):基本用 ...

  2. Redux 入门教程(二):中间件与异步操作

    上一篇文章,介绍了 Redux 的基本做法:用户发出 Action,Reducer 函数算出新的 State,View 重新渲染. 但是,一个关键问题没有解决:异步操作怎么办?Action 发出以后, ...

  3. [转] Redux入门教程(快速上手)

    学习前提 在我们开始以前,确保你熟悉以下知识: 函数式JavaScript 面向对象JavaScript JavaScript ES6 语法 同时,确保你的设备已经安装: NodeJS Yarn(或者 ...

  4. Redux 入门教程(三):React-Redux 的用法

    为了方便使用,Redux 的作者封装了一个 React 专用的库 React-Redux,本文主要介绍它. 这个库是可以选用的.实际项目中,你应该权衡一下,是直接使用 Redux,还是使用 React ...

  5. Redux 入门教程(一):基本用法

    转自http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html(仅供个人学习使用) 首先明确一点, ...

  6. redux使用教程详细介绍

    本文介绍redux的使用 安装 cnpm install redux --save cnpm install react-redux --save cnpm install redux-devtool ...

  7. Redux 入门到高级教程

    Redux 是 JavaScript 状态容器,提供可预测化的状态管理. (如果你需要一个 WordPress 框架,请查看 Redux Framework.) Redux 除了和 React 一起用 ...

  8. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  9. 实例讲解react+react-router+redux

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

随机推荐

  1. seajs引入jquery框架问题

    seajs引入jquery框架时出现的问题 原因:由于seajs是遵循cmd规范,而jquery是遵循amd规范,所以在seajs调用jquery框架时,需要将amd转换为cmd 转换方法:jquer ...

  2. Windows10 64位安装TensorFlow-GPU

    TensorFlow有GPU版和CPU版. GPU版需要CUDA和cuDNN支持,到链接:https://developer.nvidia.com/cuda-gpus 确认自己的显卡是否支持CUDA. ...

  3. Js/使用js来改变图片的url

    1.使用js的方式来改变url地址: $('#a1').attr("src","test1.jpg");这种方式来改变图片的url地址: 而不是采用$('#a1 ...

  4. linux command 2

    #修改文件所有权为user group chown user:group file/group 根据:的位置判断是修改所有者还是组 #修改文件读写执行属性 chmod mask file/group ...

  5. php7之严格模式RFC

    首先需要开启严格模式: declare(strict_types = ); 严格模式下,形参和返回值可加限制.对返回值的限制需要在参数的()后面加上引号加类型限制即可,例: function demo ...

  6. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  7. manhattan plots in qqplot2

    ###manhattan plots in qqplot2library(ggplot2)setwd("~/ncbi/zm/XPCLR/")read.table("LW. ...

  8. Linq中的左连,右连,内连

    1.左连接: var LeftJoin = from emp in ListOfEmployeesjoin dept in ListOfDepartmenton emp.DeptID equals d ...

  9. Okhttp、Volley和Gson的简单介绍和配合使用

    转载自:http://www.apkbus.com/home.php?mod=space&uid=784586&do=blog&id=61255 1.okhttp是一个高效的. ...

  10. python:Hamlet英文词频统计

    #CalHamletV1.py def getText(): #定义函数读取文件 txt = open("hamlet.txt","r").read() txt ...