1 安装react-redux: npm install --save react-redux

2.之前使用redux的store.subscribe监听 store的状态改变,通过store.getState函数获取store的状态值;并且需要划分ui组件和聪明组件,着实很麻烦,引入react-redux,可以改变这一切;

store定义如下,引入react-redux:

import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducer'; const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer, composeEnhancers(
applyMiddleware(thunk)
)); export default store;

3. 顶部组件在最外层引入store,这样所有的组件都可以使用store了;

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { Provider } from 'react-redux'
import store from './store'
const MyApp = (
<Provider store={store}>
<App/>
</Provider>
)
ReactDOM.render(MyApp, document.getElementById('root'));

4.具体在组件中使用connect将 ui组件变成聪明组件:

import React, { Component } from 'react';
import './App.css';
import { connect } from 'react-redux'
import {changeInputValue,addInputValue,deleteListItem,getListData} from './store/actionCreator.js'
class App extends Component {
constructor(props){
super(props);
this.state = {}
}
componentDidMount(){
this.props.getListDatas();
}
render() {
const {inputValue,changeInputValue,addListItem,list,deleteList} = this.props;
return (
<div className="App">
<label htmlFor="box">
输入信息
<input id="box"
value = {inputValue}
onChange ={changeInputValue}
/>
<button onClick={addListItem}>提交</button>
</label>
<ul>
{
list.map((item,index) => {
return (
<li key={index} onClick={deleteList.bind(this,index) }>{item}</li>
)
})
}
</ul>
</div>
);
} }
const mapStateToProps = (state)=> {
return (
{
inputValue:state.inputValue,
list:state.list
}
)
};
const mapDispatchToProps = (dispatch)=>{
return {
changeInputValue(e){
dispatch(changeInputValue(e.target.value))
},
addListItem(){
dispatch(addInputValue())
},
deleteList(index){
dispatch(deleteListItem(index))
},
getListDatas(){
dispatch(getListData())
}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

react-redux 的使用的更多相关文章

  1. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  2. react+redux教程(六)redux服务端渲染流程

    今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...

  3. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

    今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...

  4. react+redux官方实例TODO从最简单的入门(6)-- 完结

    通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...

  5. react+redux官方实例TODO从最简单的入门(1)-- 前言

    刚进公司的时候,一点react不会,有一个需求要改,重构页面!!!完全懵逼,一点不知道怎么办!然后就去官方文档,花了一周时间,就纯react实现了页面重构,总体来说,react还是比较简单的,由于当初 ...

  6. 重写官方TodoList,对于初学react+redux的人来说,很有好处

    虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...

  7. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  8. react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性

    reduce().filter().map().some().every()....展开属性   这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...

  9. react+redux教程(二)redux的单一状态树完全替代了react的状态机?

    上篇react+redux教程,我们讲解了官方计数器的代码实现,react+redux教程(一).我们发现我们没有用到react组件本身的state,而是通过props来导入数据和操作的. 我们知道r ...

  10. react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware

    今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...

随机推荐

  1. FFT模板(无讲解)

    #include<bits/stdc++.h> using namespace std; ; const double pi=3.1415926535898; ],len; struct ...

  2. mac以及centos下安装Elasticsearch 以及权限管理插件

    Elasticsearch安装(提前系统需要安装java环境)mac安装 brew install elasticsearch centos安装 下载ElasticSearch安装包,https:// ...

  3. Java面向对象的三大特性之一 多态

    多态: 子类重写父类方法 1)位置:子类和父类中有同名的方法 2)方法名相同,返回类型和修饰符相同,参数列表相同       方法体不同 多态的优势和应用场合 多态:同一个引用类型,使用不同的实例而执 ...

  4. Top k问题的讨论(三种方法的java实现及适用范围)

    在很多的笔试和面试中,喜欢考察Top K.下面从自身的经验给出三种实现方式及实用范围. 合并法 这种方法适用于几个数组有序的情况,来求Top k.时间复杂度为O(k*m).(m:为数组的个数).具体实 ...

  5. 2.6 C++通过引用来传递和返回类对象

    参考:http://www.weixueyuan.net/view/6338.html 总结: C++语言中,由类声明的对象,和其它类型声明的变量一样,同样可以通过传值.引用和指针的方式作为函数的参数 ...

  6. DevExpress ASP.NET v18.2新功能详解(一)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...

  7. IntelliJ IDEA导入Javax包(servlet-api.jar)

    在初次使用 IntelliJ IDEA 中,当你使用javax.servlet包下的类时(例:javax.servlet.http.HttpServlet), 在你会发现在IntelliJ IDEA里 ...

  8. python 高级语言特性

    装饰器decorator的使用 在某公司的一次笔试中面试官出了一道题,使用python 的decorator实现一个函数的执行时间的计算. 分析:关于函数执行时间的计算,那么肯定是执行之前得到一个时间 ...

  9. get 和post 请求的写法

    get请求 import requests base_url = 'http://httpbin.org' # 定义请求所需的参数,参数之间以英文逗号隔开 param_data = {'} # 发送G ...

  10. 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)

    描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...