react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
今天,我们通过解读官方示例代码(counter)的方式来学习react+redux。
例子

这个例子是官方的例子,计数器程序。前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一(延迟一秒)。
源代码:https://github.com/lewis617/react-redux-tutorial/tree/master/redux-examples/counter
组件
components/Counter.js
import React, { Component, PropTypes } from 'react'
class Counter extends Component {
render() {
//从组件的props属性中导入四个方法和一个变量
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props;
//渲染组件,包括一个数字,四个按钮
return (
<p>
Clicked: {counter} times
{' '}
<button onClick={increment}>+</button>
{' '}
<button onClick={decrement}>-</button>
{' '}
<button onClick={incrementIfOdd}>Increment if odd</button>
{' '}
<button onClick={() => incrementAsync()}>Increment async</button>
</p>
)
}
}
//限制组件的props安全
Counter.propTypes = {
//increment必须为fucntion,且必须存在
increment: PropTypes.func.isRequired,
incrementIfOdd: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
//counter必须为数字,且必须存在
counter: PropTypes.number.isRequired
};
export default Counter
上述代码,我们干了几件事:
- 从props中导入变量和方法
- 渲染组件
有的同学可能会急于想知道props的方法和变量是怎么来,下面我们继续解读。
容器
containers/App.js
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Counter from '../components/Counter'
import * as CounterActions from '../actions/counter'
//将state.counter绑定到props的counter
function mapStateToProps(state) {
return {
counter: state.counter
}
}
//将action的所有方法绑定到props上
function mapDispatchToProps(dispatch) {
return bindActionCreators(CounterActions, dispatch)
}
//通过react-redux提供的connect方法将我们需要的state中的数据和actions中的方法绑定到props上
export default connect(mapStateToProps, mapDispatchToProps)(Counter)
看到这里,很多刚接触redux同学可能已经晕了,我来图解下redux的流程。
state就是数据,组件就是数据的呈现形式,action是动作,action是通过reducer来更新state的。
上述代码,我们干了几件事:
- 把state的counter值绑定到props上
- 把四个action创建函数绑定到props上
connect
那么为什么就绑定上去了呢?因为有connect这个方法。这个方法是如何实现的,或者我们该怎么用这个方法呢?connect这个方法的用法,可以直接看api文档。我也可以简单描述一下:
- 第一个参数,必须是function,作用是绑定state的指定值到props上面。这里绑定的是counter
- 第二个参数,可以是function,也可以是对象,作用是绑定action创建函数到props上。
- 返回值,是绑定后的组件
这里还有很多种其他写法,我喜欢在第二个参数绑定一个对象,即
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Counter from '../components/Counter'
import * as CounterActions from '../actions/counter'
//将state.counter绑定到props的counter
function mapStateToProps(state) {
return {
counter: state.counter
}
}
//通过react-redux提供的connect方法将我们需要的state中的数据和actions中的方法绑定到props上
export default connect(mapStateToProps, CounterActions)(Counter)
还可以不写第二个参数,后面用dispatch来触发action的方法,即
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Counter from '../components/Counter'
import * as CounterActions from '../actions/counter'
//将state.counter绑定到props的counter
function mapStateToProps(state) {
return {
counter: state.counter
}
}
//通过react-redux提供的connect方法将我们需要的state中的数据绑定到props上
export default connect(mapStateToProps)(Counter)
后面在组件中直接使用dispatch()来触发action创建函数。
action和reducer两个好基友负责更新state
actions/counter.js
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER'
//导出加一的方法
export function increment() {
return {
type: INCREMENT_COUNTER
}
}
//导出减一的方法
export function decrement() {
return {
type: DECREMENT_COUNTER
}
}
//导出奇数加一的方法,该方法返回一个方法,包含dispatch和getState两个参数,dispatch用于执行action的方法,getState返回state
export function incrementIfOdd() {
return (dispatch, getState) => {
//获取state对象中的counter属性值
const { counter } = getState()
//偶数则返回
if (counter % 2 === 0) {
return
}
//没有返回就执行加一
dispatch(increment())
}
}
//导出一个方法,包含一个默认参数delay,返回一个方法,一秒后加一
export function incrementAsync(delay = 1000) {
return dispatch => {
setTimeout(() => {
dispatch(increment())
}, delay)
}
}
//这些方法都导出,在其他文件导入时候,使用import * as actions 就可以生成一个actions对象包含所有的export
reducers/counter.js
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions/counter'
//reducer其实也是个方法而已,参数是state和action,返回值是新的state
export default function counter(state = 0, action) {
switch (action.type) {
case INCREMENT_COUNTER:
return state + 1
case DECREMENT_COUNTER:
return state - 1
default:
return state
}
}
reducers/index.js
import { combineReducers } from 'redux'
import counter from './counter'
//使用redux的combineReducers方法将所有reducer打包起来
const rootReducer = combineReducers({
counter
})
export default rootReducer
上述代码我们干了几件事:
- 写了四个action创建函数
- 写了reducer用于更新state
- 将所有reducer(这里只有一个)打包成一个reducer
看到这里,有很多初次接触redux的同学可能已经晕了,怎么那么多概念?为了形象直观,我们在开发工具(react dev tools)上看看这些state,props什么的:
action的方法和state的变量是不是都绑定上去了啊。state怎么看呢?这个需要借助redux的开发工具,也可以通过Connect(Counter)组件的State来查看redux那颗全局唯一的状态树:
那个storeState就是全局唯一的状态树。我们可以看到只有一个counter而已。
注册store
store/configureStore.js
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import reducer from '../reducers'
//applyMiddleware来自redux可以包装 store 的 dispatch
//thunk作用是使action创建函数可以返回一个function代替一个action对象
const createStoreWithMiddleware = applyMiddleware(
thunk
)(createStore)
export default function configureStore(initialState) {
const store = createStoreWithMiddleware(reducer, initialState)
//热替换选项
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextReducer = require('../reducers')
store.replaceReducer(nextReducer)
})
}
return store
}
index.js
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import configureStore from './store/configureStore'
const store = configureStore()
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
上述代码,我们干了几件事:
- 用中间件使action创建函数可以返回一个function代替一个action对象
- 如果在热替换状态(Webpack hot module replacement)下,允许替换reducer
- 导出store
- 将store放进provider
- 将provider放在组件顶层,并渲染
applyMiddleware、thunk
applyMiddleware来自redux可以包装 store 的 dispatch()
thunk作用使action创建函数可以返回一个function代替一个action对象
服务
server.js
var webpack = require('webpack')
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
var config = require('./webpack.config')
var app = new (require('express'))()
var port = 3000
var compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
app.use(webpackHotMiddleware(compiler))
app.get("/", function(req, res) {
res.sendFile(__dirname + '/index.html')
})
app.listen(port, function(error) {
if (error) {
console.error(error)
} else {
console.info("==>
react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware的更多相关文章
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- react+redux教程(二)redux的单一状态树完全替代了react的状态机?
上篇react+redux教程,我们讲解了官方计数器的代码实现,react+redux教程(一).我们发现我们没有用到react组件本身的state,而是通过props来导入数据和操作的. 我们知道r ...
- react+redux教程(八)连接数据库的redux程序
前面所有的教程都是解读官方的示例代码,是时候我们自己写个连接数据库的redux程序了! 例子 这个例子代码,是我自己写的程序,一个非常简单的todo,但是包含了redux插件的用法,中间件的用法,连接 ...
- react+redux教程(七)自定义redux中间件
今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...
- react+redux教程(六)redux服务端渲染流程
今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...
- react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- react/redux组件库、模板、学习教程
开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...
- 使用react+redux+react-redux+react-router+axios+scss技术栈从0到1开发一个applist应用
先看效果图 github地址 github仓库 在线访问 初始化项目 #创建项目 create-react-app applist #如果没有安装create-react-app的话,先安装 npm ...
随机推荐
- C#窗体中读取修改xml文件
由于之前没有操作过xml文件,尤其是在窗体中操作xml,脑子一直转不动,而且很抵制去做这个功能,终于还是突破了自己通过查询资料完成了这个功能,在此记录一下自己的成果. 功能说明:程序中存在的xml文件 ...
- mac与windows通过ftp传输文件
1.两个系统相互传文件,比较通用的方式是用QQ,两台电脑一台各登陆一个qq,发文件就行了,在同一个网段时,qq会自动转换为按局域网的方式传输. 2.本人不愿安装qq,以ftp方式进行传输,先在wndo ...
- iOS 添加中文支持的操作
1.选择工程菜单,这里要选中Project,而不是Targets 2.点击Info菜单, 下拉到最后,看到Localizations. 点击+号. 3.选择中文 chinese-simplif ...
- .net网站常见问题及解决方案
1. 解决方法: 三个文件放到项目Bin文件夹 2. 解决方法 : requestValidationMode="2.0" 加到 3. 你在浏览器输入网址报这样的错误 ...
- error C2678
自定义结构类型,为支持插入到stl set或者排序,一种方式是重载operator<运算符成员函数.如果忘记将函数标识为const,则在编译时会报 6>c:\program files ( ...
- 跨平台移动开发UI语言 -XAML
Xamarin.Forms 把XAML (Extensible Application Markup Language, XAML) 带进了ios,android的界面开发,也就使得使用Xamarin ...
- ReactJS实践(一)—— FrozenUI React化之Loading组件
在前面我们通过四篇文章入门了React的大部分主要API,现在则开始进入实践环节. 实践系列的开篇打算拿我司的FrozenUI来试验,将其部分UI组件进行React化,作为第一篇实践文章,将以较简单的 ...
- Google分布式构建软件之三:分布式执行构建步骤
注:本文英文原文在google开发者工具组的博客上[需要FQ],以下是我的翻译,欢迎转载,但请尊重作者版权,注名原文地址. 之前两篇文章分别介绍了Google 分布式软件构建系统Blaze相关的为了提 ...
- 用.NET MVC实现长轮询,与jQuery.AJAX即时双向通信
两周前用长轮询做了一个Chat,并移植到了Azure,还写了篇博客http://www.cnblogs.com/indream/p/3187540.html,让大家帮忙测试. 首先感谢300位注册用户 ...
- [nodemon] Internal watch failed: watch ENOSPC错误解决办法
运行环境:Ubuntu 16.04, WebStorm 2016.1.3, node.js v0.12.5, nodemon 1.9.2 在WS自带的终端输入nodemon server.js启动项目 ...