复杂以后 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. 微信公众平台-文章-app:让创意变为现实,这30个小程序“凭什么”脱颖而出?

    ylbtech-微信公众平台-文章-app:让创意变为现实,这30个小程序“凭什么”脱颖而出? 1.返回顶部 1. 让创意变为现实,这30个小程序“凭什么”脱颖而出? 微信公开课 昨天 创意1:为多种 ...

  2. 解决Linux主机上的 远程MySQL客户端无法连接的问题

    无法连接到 MySQL 数据库可能的原因有: 1. PHP 无法连接 MySQL 可能是 PHP 配置不正确,没加上连接 MySQL 的功能. 2. MySQL 软件包升级,但没有升级数据库,或安装 ...

  3. Postgresql个人维护库时,出现有用户在连接又找不到这个用户是谁的强制中断连接的方法;

    方法一: 去PostgreSQL目录下/data/pgdata/9.4,找到pg_hba.conf, 修改pg_hba.conf的白名单IP (修改前,最好服务已停止,我是这么操作的) # IPv4 ...

  4. hdu 1348【凸包模板】

    #include<iostream> #include<iostream> #include<algorithm> #include<cmath> us ...

  5. codeforces912E(折半搜索+双指针+二分答案)

    E. Prime Gift E. Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes inpu ...

  6. MFC中利用CString和Format成员函数将数字格式化输出

    str.Format("格式控制字符串”,输出列表): 格式控制字符串包括格式字符串和非格式字符串,用双引号括起来.其中非格式字符串原样输出. 格式字符串是以%开头的字符串:%[标识][输出 ...

  7. [poj2096] Collecting Bugs【概率dp 数学期望】

    传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...

  8. bryce1010的图像处理课程设计

    一.要求 完成课程教学中的大部分图像处理功能 二.平台 Qt c++ windows或者linux下 三.思路收集 1.QPixmap类 (一)QPixmap和QImage的区别 QPixmap是专门 ...

  9. 51nod 1154 回文串划分

    1154 回文串划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有 ...

  10. 转-UIButton定义和设置圆角

    //login button // .h 中定义 UIButton *_loginBtn; @property (strong,nonatomic)UIButton *loginBtn; // .m ...