复杂以后 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. Navicat: Can't create a procedure from within another stored routine

    测试调用mysql的存储过程,于是用Navicat写,结果报这个错误,源代码如下: CREATE PROCEDUREQueryDate() BEGIN       SELECTCURDATE(); E ...

  2. mysql 里面的isnull()和ifnull() is null 和 is not null

    usergrade表 1.找到里面username是null的行 SELECT * FROM usergrade WHERE ISNULL(USERNAME) SELECT * FROM usergr ...

  3. P3162 [CQOI2012]组装

    传送门 退火大法好 我并不会正解于是只好打退火了--其他没啥好讲--只要对每一种颜色开一个vector,存一下所有这个颜色的位置,判定的时候可以去所有的颜色里二分找到前缀和后缀,把和当前点距离小的加入 ...

  4. 支持宕机自动恢复触发一次性或周期性任务执行的组件包首次介绍-easyTask

    easyTask介绍 一个方便触发一次性或周期性任务执行的工具包,支持海量,高并发,高可用,宕机自动恢复任务 使用场景 需要精确到秒的某一时刻触发任务执行.比如订单交易完成24小时后如果客户未评价,则 ...

  5. gRPC入门

    一.gRPC简介 在介绍gRPC之前先说一下RPC(Remote Procedure Call),也叫远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.相比 ...

  6. Qt下存储读写应用程序设置的三种方法

    一.简介 用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小.位置和密码等等.有三种方法可以实现: 使用注册表: 使用配置文件(.ini): 使用自定义文件(例如.txt) ...

  7. poj 2349 Arctic Network(最小生成树的第k大边证明)

    题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部 ...

  8. [USACO 2012 Mar Gold] Large Banner

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=127 又是一道这种题目,遇到一次跪一次,这次终于硬着头皮看懂了题解,但是谢 ...

  9. Codeforces Round #459 (Div. 2)C. The Monster

    C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. 构造 HDOJ 5414 CRB and String

    题目传送门 题意:给两个字符串s,t,可以在s字符串任意位置后面插入字符c(与前面的不同),问是否能够将s转换为t字符串 构造:首先lens > lent 或者 s[1] != t[1] 一定是 ...