react-redux 的使用
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 的使用的更多相关文章
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- react+redux教程(六)redux服务端渲染流程
今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- react+redux官方实例TODO从最简单的入门(6)-- 完结
通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...
- react+redux官方实例TODO从最简单的入门(1)-- 前言
刚进公司的时候,一点react不会,有一个需求要改,重构页面!!!完全懵逼,一点不知道怎么办!然后就去官方文档,花了一周时间,就纯react实现了页面重构,总体来说,react还是比较简单的,由于当初 ...
- 重写官方TodoList,对于初学react+redux的人来说,很有好处
虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- react+redux教程(三)reduce()、filter()、map()、some()、every()、...展开属性
reduce().filter().map().some().every()....展开属性 这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...
- react+redux教程(二)redux的单一状态树完全替代了react的状态机?
上篇react+redux教程,我们讲解了官方计数器的代码实现,react+redux教程(一).我们发现我们没有用到react组件本身的state,而是通过props来导入数据和操作的. 我们知道r ...
- react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
随机推荐
- Oracle exists 和not exists 用法详解
有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...
- Java 几种showMessageDialog的表示
最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...
- java阶段学习目标
0-1年: <java编程思想> 1-2年: <大话设计模式>http://www.cnblogs.com/zuoxiaolong/p/pattern26.html <重 ...
- swiftlint 你所要知道的所有!!
swiftin Should the opening brace of a function or control flow statement be on a new line or not ?:) ...
- leetcode python 005
## 给定字符串,寻找最长回文子串## 单回文,双回文 def findh(s): ## 单回文 ld,l=[],len(s) if len(s)<3: re ...
- netty源码理解补充 之 DefaultChannelPipeline到底是个啥
- <Codis><JedisPool><DeadLock>
Overview Background: I start a thread [call thread A below]in Spark driver to handle opening files i ...
- ubuntu安装scrapy方法
sudo apt-get install python-dev [默认安装python2] sudo apt-get install python3-dev [指定安装python3最新的] ...
- python josn转换方法-字典
python_json常用的方法 1. 什么是JSON? JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符 ...
- Python 依赖关系
class Person: def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 tools.run() print("很开心, 我能玩儿游戏了&quo ...