复杂以后 setState 就不太方便了

所以使用Redux来管理 React只负责View。

Store、State、Dispatch、Reducer

reducer(state,action)

{

switch(action.type){

case ...

... ...

return newAction;

}

}

·通过Reducer创建Store

·Store.dispatch(action)来修改状态

·Reducer函数接受state和action,返回新的state,可用store.subscribe监听每次修改

function listener() {
const current = store.getState()
console.log(current);
}
store.subscribe(listener)
store.dispatch({ type: '...' })
store.dispatch({ type: '...' })
 
 
 
二、Redux配合React使用
 
index.js:
const store = createStore(reducer)
<App store={store}/>
 
App.js:
 
render(){
const num = this.props.store
return({num})
}
 
三、Redux处理异步
 
import thunk from 'redux-thunk'
 
import { createStore, applyMiddleware } from 'redux'
const store = createStore(reducer,applyMiddleware(thunk))
 
//需要提交action的地方用dispatch执行一下
Action(){
     return  dispatch=>{
 
          setTimeout(()=>{
               dispatch({type:'ADD_GUN'})
          },2000)
 
     }
}
 
store.dispatch(Action())
 
四、调试工具
Chrome中的redux dev tool插件
 
import { createStore, applyMiddleware, compose } from 'redux'
 
const store = createStore(counter, compose(
applyMiddleware(thunk),
window.devToolsExtension ? window.devToolsExtension() : null
))
 
五、使用react-redux
 
//从现在开始忘记store.subscribe
 
Provider 和 Connect接口
 
·Provider放在组件最外层,传入store即可,只用一次
import { Provider } from 'react-redux'
 
<Provider store={store}>
 <App />
</Provider>
 
在app.js中:
 
import { connect } from 'react-redux'
 
const mapStatetoProp = (state) => {
return { num: state }
}
 
const actionCreators = { addGUN, removeGUN, addGUNAsync }
 
//装饰器
App = connect(mapStatetoProp, actionCreators)(App)
 
然后,
<button onClick={addGUN}>申请武器</button>
//不需要store.dispatch(addGUN())了  
注:addGUN是一个action creator
 
 

mapStateToProps会订阅 Store,每当state更新的时候,就会自动执行,重新计算 UI 组件的参数,从而触发 UI 组件的重新渲染。

mapStateToProps的第一个参数总是state对象,还可以使用第二个参数,代表容器组件的props对象。

装饰器

 
@connect([mapStateToProps], [mapDispatchToProps], [mergeProps],[options])
 
mapStateToProps(state, ownProps) : stateProps

这个函数允许我们将 store 中的数据作为 props 绑定到组件上。

mapDispatchToProps(dispatch, ownProps): dispatchProps

connect 的第二个参数是 mapDispatchToProps,它的功能是,将 action 作为 props 绑定到组件上,也会成为 MyComp 的 props。

 
总结
connect内第一个是state属性
第二个是你要什么方法
Prover放在最外面store传到属性中
 
 
六、React-router4
·BrowserRouter包裹整个应用
·Router路由对应渲染的组件,可嵌套
·Link跳转专用
·Switch
·Redirect
 

React开发实时聊天招聘工具 -第四章 Redux的更多相关文章

  1. React开发实时聊天招聘工具 -第三章 React基础知识回顾

    function a (props) { return <h1>hello world{this.props.asd}</h1> } class a extends React ...

  2. React开发实时聊天招聘工具 -第六章 登陆注册(2)

    1.bodyParser和cookieParser:   const bodyParser = require('body-parser') const cookieParser = require( ...

  3. React开发实时聊天招聘工具 -第六章 登陆注册(1)

    1.基于cookie的用户认证 express 依赖 cookie-parser 2.axios语法: axios.get('/data').then(res=>{ if(res.status= ...

  4. React开发实时聊天招聘工具 -第五章 需求分析

    Axios的使用 axios.get('/data') .then(res=>{ if(res.status==200) this.setState(data:res.data) })

  5. React开发实时聊天招聘工具 -第一章

    第一章 课程道学 6个页面 弱化css Antd-mobile作为组件库 Redux 状态管理 React-Router 路由 Axios异步请求 后端Express框架 Socket.io 数据库: ...

  6. React开发实时聊天招聘工具 -第二章

    2-1 介绍React开发环境 npm install -g create-react-app xxx npm run eject   来配置webpack 2-2 ES6常用语法 其他 还有一些特性 ...

  7. react+redux+react-router+node.js 开发实时聊天App 学习记录

    一.课程导学 1.React 主要解决的是UI层的问题,应用的状态需要借助Redux等状态管理. 2.前端React  +  antd-mobile UI组件库 + Redux 状态管理库 + Rea ...

  8. Vue2.5开发去哪儿网App 第四章笔记 上

    一 .  组件细节知识点 1.  解决组件在h5中编码规范 例如 : table , ul , ol  等等 <table> <tbody> <row></r ...

  9. Vue2.5开发去哪儿网App 第四章笔记 下

    1.解决非父子组件之间的传值问题 非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性 Vue.prototype.bus ...

随机推荐

  1. ubuntu 12.04上安装QQ2013(转载)

    转自:http://www.cnblogs.com/wocn/p/linux_ubuntu_QQ_install.html 环境介绍: OS:Ubuntu12.04 64bit QQ:WineQQ20 ...

  2. Linux下 SSH远程管理服务

    第1章 SSH基本概述 1.1 SSH服务协议说明 SSH 是 Secure Shell Protocol 的简写,由 IETF 网络工作小组(Network Working Group )制定 在进 ...

  3. zoj 2760 How Many Shortest Path【最大流】

    不重叠最短路计数. 先弗洛伊德求一遍两两距离(其实spfa或者迪杰斯特拉会更快但是没必要懒得写),然后设dis为st最短距离,把满足a[s][u]+b[u][v]+a[v][t]==dis的边(u,v ...

  4. [POI2007]旅游景点atr

    Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之 ...

  5. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. 转 ORACLE-016:ora-01720 授权选项对于'xxxx'不存在

    报错的情形如下, A用户:视图V_A B用户:视图V_B,并且用到了V_A C用户:需要用V_B, 授权过程, A用户下: grant select on V_A to B B用户下: grant s ...

  7. 172 Factorial Trailing Zeroes 阶乘后的零

    给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...

  8. Java操作pdf: JarsperReport的简单使用

    在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...

  9. git + git flow 的简单介绍

    1.git简单实用 git:是一种分布式版本控制系统,因为其优秀的特性个人十分推崇. 1.1设置本机用户身份 git config -global user.name "userName&q ...

  10. 鸢尾花数据集-iris.data

    iris.data 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3. ...