使用react-redux开发的简单步骤
一、安装相关包
npm install redux react-redux --save
二、根据具体情形创建模块文件
Store.js、Reducer.js、Actions.js
Store.js的作用在于引出store,方便在使用的时候引入。
Reducer.js的作用在于定义reducer,最好用combineReducers拆分reducer。
Actions.js用于定义ActionCreator,方便在dispatch action的时候根据不同变量生成不同action。
三、使用Provider组件以便于所有组件都能直接使用store
在使用根组件(通常是入口文件index.js)处引入Provider组件。
在index.js中引入store,
在ReactDOM.render()方法的第一个参数处使用Provider包裹根组件,
然后给Provider组件的store属性赋值为之前引入的store。
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { Provider } from 'react-redux';
import store from './Store'; ReactDOM.render(<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));
registerServiceWorker();
四、编写子组件
先写UI Component,即通常所写的普通组件;
然后定义Container Component,使用connect方法(从react-redux包中引入)即可将UI Component转化为Container Component。
注意要传递mapStateToProps、mapDispatchToProps两个参数。
最后导出组件以便于使用的时候引入。
import React from 'react';
import { connect } from 'react-redux';
import { toggleItemAction, removeItemAction } from './Reducer'; class Display extends React.Component {
componentWillMount(){
this.toggleCompleted = this.props.toggleCompleted;
this.removeItem = this.props.removeItem;
console.log(this.props);
}
render(){
return <div>
<ul>
{
this.props.items.map(item => {
let tmp = null;
if(item.completed){
tmp = <del>{item.value}</del>
}
else {
tmp = item.value;
}
if(tmp === null || tmp ===''){
return null;
}
return <li key={item.id}>
{tmp}
<button id={'toggle' + item.id} onClick={this.toggleCompleted}>Toggle</button>
<button id={'remove' + item.id} onClick={this.removeItem}>Remove</button>
</li>
})
}
</ul>
</div>
}
} const mapStateToProps = (state) => {
return {
items: state.items
};
}; const mapDispatchToProps = (dispatch) => {
return {
toggleCompleted(e){
let id = e.target.id.substring(6);
dispatch(toggleItemAction(id));
},
removeItem(e){
let id = e.target.id.substring(6);
dispatch(removeItemAction(id));
}
};
}; const DisplayContainer = connect(mapStateToProps, mapDispatchToProps)(Display); export default DisplayContainer;
五、注意点
1.mapStateToProps参数其实是一个函数,默认带有一个参数为state,作用是将state中的值映射到我们定义的组件的props属性中。
一下定义一个mapStateToProps:
const mapStateToProps = (state) => {
return {
items: state.items
};
};
这样的话,在组件中就可以通过this.props.items获取到state.items。
mapStateToProps函数所返回的对象可以认为是组件props属性的一个子集。
2.mapDispatchToProps参数是一个对象或者函数,我习惯于写成一个函数,函数具有一个默认属性即为dispatch,相当于store.dispatch(),mapDispatchToProps函数会返回一 个对象。对象中的所有方法都会传递给组件的props属性。也可以认为它所返回的对象是组件props属性的一个子集。
const mapDispatchToProps = (dispatch) => {
return {
toggleCompleted(e){
let id = e.target.id.substring(6);
dispatch(toggleItemAction(id));
},
removeItem(e){
let id = e.target.id.substring(6);
dispatch(removeItemAction(id));
}
};
};
以上代码定义了一个mapDispatchToProps函数,这样,在组件内部可以通过this.props[propertyName]访问到toggleCompleted函数或者removeItem函数。
3.如何在mapDispatchToProps函数定义的一系列函数中获取组件内部数据?
解决方法有两种:
(1)通过event.target来获取事件发生节点,然后通过DOM属性获取组件内部的数据。注意点2中的代码即是例子。
(2)在组件内部定义事件处理函数,不直接利用this.props[propertyName]来处理组件事件。组件内部自定义事件处理函数可以通过this等获取到组件内部数据,然后在组件自定义事件处理函数内部调用this.props[propertyName],传递参数即可。
4.是否需要使用react-redux
react-redux是redux的作者为了方便于在React中使用redux而开发出来的。
react-redux为我们在react项目中使用redux带来了便利,但也提升了学习成本,我们需要学习一些新的API,并且遵循react-redux的开发及组件拆分规范。
如果你因为单纯的在项目中使用redux需要不断地引入store,不断地写store.[propertyName]而感到厌烦,那么可以尝试使用react-redux。
如果你认为使用redux十分方便简洁明了,那么可能并不需要使用react-redux。
使用react-redux开发的简单步骤的更多相关文章
- 使用redux开发的简单步骤
一.安装redux包 npm install redux --save 二.根据APP数据结构或者后台请求的数据结构拟定state的大致结构. 可以把state写成一个对象字面量,放在reducer文 ...
- React+Redux开发实战项目【美团App】,没你想的那么难
README.md 前言 开始学习React的时候,在网上找了一些文章,读了官网的一些文档,后来觉得React上手还是蛮简单的, 然后就在网上找了一个React实战的练手项目,个人学完之后觉得这个项目 ...
- react+redux开发谷歌插件
React Developer Tools Redux Dev Tools
- react打包开发文件的步骤(上传给线上环境)
cd进入ReleaseProject目录,然后运行npm start,系统会自动在public目录下面完成打包工作,然后我再把 public文件下压缩位public.rar上传即可:(public文 ...
- 实例讲解基于 React+Redux 的前端开发流程
原文地址:https://segmentfault.com/a/1190000005356568 前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 s ...
- React+Redux学习笔记:React+Redux简易开发步骤
前言 React+Redux 分为两部分: UI组件:即React组件,也叫用户自定义UI组件,用于渲染DOM 容器组件:即Redux逻辑,处理数据和业务逻辑,支持所有Redux API,参考之前的文 ...
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- webpack+react+redux+es6开发模式---续
一.前言 之前介绍了webpack+react+redux+es6开发模式 ,这个项目对于一个独立的功能节点来说是没有问题的.假如伴随着源源不断的需求,前段项目会涌现出更多的功能节点,需要独立部署运行 ...
随机推荐
- C#中要使ListBox使用AddRange()时,能够触发SelectedValueChanged事件
1. 要触发 SelectedValueChanged事件,必须要当ListBox所选中的值发生改变 基本思路是: 当AddRange()后,就马上指定ListBox的SelectedIndex,这样 ...
- 部分linux命令
计算机网络的主要优点是能够实现资源和信息的共享,并且用户可以远程访问信息.Linux提供了一组强有力的网络命令来为用户服务,这些工具能够帮助用户登录到远程计算机上.传输文件和执行远程命令等. 本章介绍 ...
- 在本地用命令行创建一个git仓库,并推送到远程
首先,进入的gitStore目录下(没有的话自己创建一个) 1.git init 在gitStore目录下 初始化一个git仓库 2.git add 复制一个文件到gitStore目录下,然后执行gi ...
- C#根据用户输入字符串,输出大写字母有几个,小写字母有几个
static void Main(string[] args) { // 根据用户输入字符串,输出大写字母有几个,小写字母有几个. Console.WriteLine("请输入一行英文代码& ...
- tomcat绑定域名绑定端口及更换ROOT目录
一.更换ROOT目录 tomcat默认网站目录为 webapps/ROOT ,那么我们如何改为自己的网站目录呢? 1.打开并编辑tomcat目录下的 conf/server.xml 大约在148行的位 ...
- CentOS 7.2 安装 MySQL 5.6.24
说明:由于甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此CentOS社区采用mysql的MariaDB分支的方式来避开这个风险. 所以需要先加入yum的仓库,才能利用yum来安装my ...
- Javascript获取页面表格中的数据
var main=mygrid.gettable("11"); //表示获取非固定列的表格 var main1=mygrid.gettable("01");// ...
- jQuery源码分析系列 : 整体架构
query这么多年了分析都写烂了,老早以前就拜读过, 不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍 我也不会照本宣科的翻译源码,结合自己的实际经验一起拜读吧! ...
- PowerDesigner设置所有int主键自增脚本
'*****************************************************************************dim model 'current mod ...
- How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
Objective The objective is to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux. This a ...