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. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
随机推荐
- HTML编辑笔记1
1.编写html ①新建一个记事本(以.html结尾) ②右击选择打开方式为文档 ③编写内容 ④用浏览器查看内容 2.html编写格式 <html> <head></he ...
- word-wrap与break-word属性的区别
共同点 word-wrap:break-word与word-break:break-all都能把长单词强行断句 不同点 word-wrap:break-word会首先起一个新行来放置长单词,新的行还是 ...
- Cracking The Coding Interview3.3
//Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in re ...
- 8.Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- 2019-03-14-day010-函数进阶
昨日回顾 1.函数: 函数的定义 函数的参数 位置参数 关键字参数 混合参数 形参的位置上(默认参数) 实参的位置上(关键字参数) 位置参数 > 默认参数 三元运算符: c = a if a&g ...
- Python 黏包及黏包解决方案
粘包现象 说粘包之前,我们先说两个内容,1.缓冲区.2.windows下cmd窗口调用系统指令 1 缓冲区(下面粘包现象的图里面还有关于缓冲区的解释) 每个 socket 被创建后,都会分配两个缓冲区 ...
- POJ 1287 Networking(最小生成树裸题有重边)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- js中call、apply和bind的区别
在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对 ...
- 设计精美Power BI报告的五大秘诀
众所周知,Power BI可以帮助您创建交互式且信息丰富的报告,但使用Power BI 制作精美而实用的报告对我们这群IT人员而言,却是一个巨大的痛苦:但个人觉得不能就此止步,通过不断实践练习,小悦采 ...
- threejs path controls example html
<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - pa ...