redux使用教程详细介绍
本文介绍redux的使用
安装
cnpm install redux --save
cnpm install react-redux --save
cnpm install redux-devtools --save-dev
如果你之前使用过vuex,我相信redux对于你来说就是易如反掌
redux官网将的很杂很乱,但是实用的东西就那么点
action
action就是一个对象,用来描述你要修改store状态树中的数据
{
type: 'change_name',
name: 'yejiawei'
}
type字段必须要有,这是约定
action创建函数
正常情况下,你需要给每一个action定义一个函数,从而方便调用
export function changeName (value) {
return {
type: 'change_name',
name: value
}
}
Reducer
Reducer的作用,就是将不同的action汇总,然后返回相应的state
const initialState = {
name: 'haha'
}
function firstDemo (state = initialState, action) {
switch (action.type) {
case "change_name":
return Object.assign({},state,{
name: action.name
})
default:
return state
}
}
返回值必须是全新的
拆分reducer
实际开发中都是模块化的,有必要将不同模块的reducer分开
import { combineReducers } from 'redux'
combineReducers({firstDemo,firstDemo1})
store
创建store是非常简单的
import { createStore } from 'redux'
将上面创建的reducer当做参数传递即可
let store = createStore(firstDemo)
store.getState() // 获取store中的数据
let unsubscribe = store.subscribe( () => {
...
} ) // 监听器
store.dispatch(changeName('yejiawei')) // 调用action修改store中的state
unsubscribe() // 注销监听器
在react组件中使用redux
下面我将列出,正常项目开发的结构
index.js
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import store from './store.js'
import App from './app.js'
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
使用 Provider 组件传递store到react组件中
app.js
import React from 'react'
import MyComponent1 from './component1.js'
import { connect } from 'react-redux'
class MyComponent extends React.Component {
render () {
return (
<div>
<MyComponent1 {...this.props}></MyComponent1>
</div>
)
}
}
function appWant(state) {
return state
}
export default connect(appWant)(MyComponent)
使用connect方法将react组件连接到redux中,接受一个回调函数,并且回调函数的参数就是store中的state
** 原则,使用connect方法尽量只在容器组件中使用,其余的子组件如果也想访问store,就通过props传递即可
store.js
import { createStore } from 'redux'
const initialState = {
message: 'yejiawei'
}
function firstApp (state = initialState, action) {
switch (action.type) {
case "change_message":
return Object.assign({},state,{
message: action.message
})
default:
return state
}
}
let store = createStore(firstApp);
export default store
此文件专门用来管理和生成store的,同时还可以将reducer专门再生成一个reducer.js管理
component1.js
此组件代表类似的子组件
import React from 'react'
import { delayData } from './actions.js'
class MyComponent extends React.Component {
componentDidMount() {
this.props.dispatch(changeMessage('我改变了'))
}
render() {
return (
<div style={{"height": "200px","width": "200px","background": "red","position": "absolute","top": "100px", "left": 0}}>我是组件一{this.props.message}</div>
)
}
}
export default MyComponent
在子组件中访问store中的state和dispatch通过props直接访问即可
actions.js
此文件专门用来处理redux中的action生成函数
export function changeMessage (text) {
return {
type: 'change_message',
message: text
}
}
在react组件中使用redux补充
上面讲到的connect方法,还可以传递其他参数
注意到我们传递给connect方法的参数是如下的这个函数
function appWant(state) {
return state
}
这个函数会在state改变的时候更新整个app组件,也就是说不管你在哪里dispatch了,那么整个app都会重新更新,性能损失
所以可以选择只传递一部分state,如下
function appWant(state,ownProps) {
return {
age: state.age
}
}
然后在其他的组件中也调用connect方法,管理自己的state,而不是只通过props传递,这样可以提高性能
另外也可以把action单独传递或者传递一部分,我不建议这样做,对性能没有任何提高,反而提升代码复杂度,直接使用dispatch简单清晰明了
在app.js文件中改成如下代码
import React from 'react'
import MyComponent1 from './component1.js'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux';
import { delayData } from './actions.js'
class MyComponent extends React.Component {
render () {
return (
<div>
<MyComponent1 {...this.props}></MyComponent1>
</div>
)
}
}
function appWant(state) {
return {
age: state.age
}
}
function funcWant(dispatch) {
return {
demo: bindActionCreators({delayData},dispatch)
}
}
export default connect(appWant,funcWant)(MyComponent)
然后再component1.js中,就不需要通过dispatch调用了
this.props.demo.delayData('我又变了');
异步action
上面讲的内容是同步的,也就是说dispatch方法调用后,store中的state立即发生改变
那么,现在有一个需求是,dispatch的写法不发生任何改变,还可以进行异步操作
如何操作?只需要将action返回一个函数,然后在函数里面进行异步处理就完事儿了
要实现这个操作就要借助 redux-thunk-middleware 中间件
安装 cnpm install --save redux-thunk
然后将上面的store.js文件改成下面的
import { createStore, applyMiddleware } from 'redux' // 导入applyMiddleware方法用来使用中间件
import thunkMiddleware from 'redux-thunk' // 导入中间件
const initialState = {
message: 'yejiawei'
}
function firstApp (state = initialState, action) {
switch (action.type) {
case "change_message":
return Object.assign({},state,{
message: action.message
})
default:
return state
}
}
let store = createStore(firstApp,applyMiddleware(thunkMiddleware)); // 将中间件应用于store中
export default store
然后再actions.js中添加一个异步action
export function delayData (value) {
return (dispatch) => {
setTimeout( () => {
dispatch(changeMessage(value))
},1000 )
}
}
最后,直接在component1.js文件中直接调用即可
import React from 'react'
import { delayData } from './actions.js'
class MyComponent extends React.Component {
componentDidMount() {
this.props.dispatch(delayData('我改变了'))
}
render() {
return (
<div style={{"height": "200px","width": "200px","background": "red","position": "absolute","top": "100px", "left": 0}}>我是组件一{this.props.message}</div>
)
}
}
export default MyComponent
异步action的补充一
上面在定义异步action时,在返回的函数里面传递了dispatch参数,其实它还支持如下的参数
还是借助上面的例子
传递getState获取store中的state
export function delayData (value) {
return (dispatch,getState) => {
setTimeout( () => {
dispatch(changeMessage(value))
console.log(getState())
},1000 )
}
}
传递自定义参数
在store中将创建store的代码改成
let store = createStore(firstApp,applyMiddleware(thunkMiddleware.withExtraArgument( {a: 'aaa', b: 'bbb'} )));
然后再actions.js中获取参数
export function delayData (value) {
return (dispatch, getState, {a,b}) => {
setTimeout( () => {
dispatch(changeMessage(value))
console.log(a,b)
console.log(getState())
},1000 )
}
}
redux使用教程详细介绍的更多相关文章
- mongodb 3.0下载安装、配置及mongodb最新特性、基本命令教程详细介绍
mongoDB简介(本文由www.169it.com搜集整理) MongoDB是一个高性能,开源,无模式的文档型数据库,是目前在IT行业非常流行的一种非关系型数据库(NoSql).它在许多场景下可用于 ...
- 【WiFi密码破解详细图文教程】ZOL仅此一份 详细介绍从CDlinux U盘启动到设置扫描破解-破解软件论坛-ZOL中关村在线
body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-ser ...
- Linux截屏工具scrot用法详细介绍
Scrot是Linux命令行中使用的截图工具,能够进行全屏.选取等操作,下面小编将针对Scrot截图工具的用法给大家做个详细介绍,通过操作实例来学习Scrot的使用. 在Linux中安装Scrot ...
- 用grunt搭建自动化的web前端开发环境实战教程(详细步骤)
用grunt搭建自动化的web前端开发环境实战教程(详细步骤) jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用!前端自动化, ...
- snoopy(强大的PHP采集类) 详细介绍
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单,可以用来开发一些采集程序和小偷程序,本文章详细介绍snoopy的使用教程. Snoopy的一些特点: 抓取网页的内容 fe ...
- WDCP是什么 关于WDCP的详细介绍
WDCP是WDlinux Control Panel的简称,是一套用PHP开发的Linux服务器管理系统以及虚拟主机管理系统,,旨在易于使用Linux系统做为我们的网站服务器,以及平时对Linux服务 ...
- Xilinx Vivado的使用详细介绍(1):创建工程、编写代码、行为仿真
Xilinx Vivado的使用详细介绍(1):创建工程.编写代码.行为仿真 Author:zhangxianhe 新建工程 打开Vivado软件,直接在欢迎界面点击Create New Projec ...
- vue对比其他框架详细介绍
vue对比其他框架详细介绍 对比其他框架 — Vue.jshttps://cn.vuejs.org/v2/guide/comparison.html React React 和 Vue 有许多相似之处 ...
- Arduino可穿戴开发入门教程LilyPad介绍
Arduino可穿戴开发入门教程LilyPad介绍 Arduino输出模块 LilyPad官方共提供了4种输出模块,他们分别是单色LED模块(图1.5).三色LED模块(图1.6).蜂鸣器模块(图1. ...
随机推荐
- 汇编笔记 RET
assume cs:code,ss:stack stack segment db dup() stack ends code segment mov ax,4c00h int 21h start: m ...
- UVA 11827 水
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- iOS系统声音服务(System Sound Services)
系统声音服务(System Sound Services)提供了一个接口,用于播放不超过30秒的声音.它支持的文件格式有限,具体地说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. ...
- css 单行和多行文本溢出显示省略号
如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; te ...
- thinkphp中如何使用phpspreadsheet插件
thinkphp中如何使用phpspreadsheet插件 一.总结 一句话总结:多百度,百度什么都有 1.thinkphp中用composer安装的插件的命名空间是什么? use PhpOffice ...
- CSS基础(float属性与清除浮动)
3.8 这是CSS里比较重要的属性:浮动,这个属性会在以后经常用到,算是一个重点吧 浮动 语法:float:left | right | none 特点: 浮动的元素不占位置,脱离了标准文档流 ...
- 报表研究之工具篇-VBA
最近一直在研究VBA,写报表的工作.将所得的知识,经验总结一下,与大家分享. 工具篇,VBA 1.EXCEL一个最好用的功能就是录制宏,当一个函数拿不准要怎么写,用什么函数表示的时候,录制就帮了大忙了 ...
- 显示隐藏文件 osx 10.10
教程原文:http://m.blog.csdn.net/blog/i0S123tianzhuang/25736223 终端命令 Finder显示隐藏文件: defaults write com.app ...
- linux服务器应用NTP配置时间同步
linux服务器应用NTP配置时间同步 • 为什么建议使用ntpd而不是ntpdate? #####原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18 ...
- 三张图较为好理解JavaScript的原型对象与原型链
最近从网上看到别人详细得讲解了js的原型对象和原型链,看完感觉是看得最清晰的一个,于是,摘录到自己博客里 对于新人来说,JavaScript的原型是一个很让人头疼的事情,一来prototype容易与_ ...