4.Redux学习4----react-redux
react-redux是配合redux一起使用的,其中核心组件是Provider
Provider是store的提供器,用Provider则store就无需直接引入组件内,而且还可以将一个store公共存储数据区域提供给多个组件
注意一下:之前store相关api导入的是redux, 而Provider,还有相关的api都是导入react-redux,需要npm i先下载
第一步:在index.js入口文件中
import React from 'react';
import ReactDOM from 'react-dom';
import TodoList from "./todolist"
import {Provider} from 'react-redux'
import store from './store'
const App = (
<Provider store={store}>
<TodoList />
{/*其他的一些组件,可以用同一store*/}
{/*<A/>*/}
{/*<B/>*/}
</Provider>
)
ReactDOM.render(App, document.getElementById('root'));
之前我们是在组件中通过store.getState()来获取store中数据中:
constructor(props){
super(props)
this.state = store.getState()
}
第二步:现在无需通过getState()方法,而是通过connct连接store和组件,并将store的数据映射到store的props上
在todolist写上如下代码:
import React,{Component} from 'react';
import store from './store'
// 别引成redux了
import {connect} from "react_redux"
class TodoList extends Component {
constructor(props){
super(props)
this.state = store.getState()
}
render() {
return (
<div>
<div>
{/*此时就要用this.props*/}
<input type="text" value={this.props.inputValue}/>
<button>提交</button>
</div>
<ul>
<li>Dell</li>
</ul>
</div>
)
}
}
// 做连接,如何做连接,就要写映射规则
// mapStateToProps里面写的是props和store中数据的映射规则
// state就是store里面的数据,回调时候会传递过来
const mapStateToProps = (state) => {
return {
// 组件的props属性对应store中的属性
inputValue: state.inputValue
}
}
// 导出前将TodoList组件和store做连接
export default connect(mapStateToProps,null)(TodoList)
以上代码基本逻辑就是:Provider包裹的并与store做连接的组件初始化时候,就会调用一次 mapStateToProps ,将store里面的数据返回给到组件的props。
而且以后每次store里面的数据更新,这个mapStateToProps都会被回调,自动更新inputValue的值.
或者理解成:把props的属性和store.state做了映射,只要state.inputValue发生改变,那么props 对应的属性inputValue就会自动被更新
运行代码:input获得到了store中的数据,如下图所示:

之前我们是用store.dispatch做分发的,但是现在组件并没有引入store,如何做派发action呢?
第三步:changeInputValue,将store.dispatch映射到props中使用
import React,{Component} from 'react'
import { connect } from "react-redux"
class TodoList extends Component {
render() {
return (
<div>
<div>
{/*此时就要用this.props*/}
<input type="text" onChange={this.props.changeInputValue} value={this.props.inputValue}/>
<button>提交</button>
</div>
<ul>
</ul>
</div>
)
}
}
// 做连接,如何做连接,就要写映射规则
// mapStateToProps里面写的是props和store中数据的映射规则
// state就是store里面的数据,回调时候会传递过来
const mapStateToProps = (state) => {
return {
// 这么写就是给组件一个props属性叫inputValue, 并将props属性对应store中的属性
inputValue: state.inputValue
}
}
// 将store.dispatch映射到props中使用
const mapDispatchToProps = (dispatch) => {
return {
// 这么写就是给组件一个props属性叫changeInputValue,而在这个changeInputValue中可以使用传递过来的dispatch
changeInputValue(e){
const action = {
type: 'change_input_value',
value: e.target.value
}
dispatch(action)
}
}
}
// 导出前将TodoList组件和store做连接
export default connect(mapStateToProps,mapDispatchToProps)(TodoList)
第四步:优化
TodoList其实这时是一个UI组件,因为本身并不包含任何逻辑代码,只是接受数据,那么将其改成无状态函数组件会更节约性能
我们说UI组件没有状态,肯定会有一个有状态的容器组件包裹,给他提供数据,其实通过connect方法就会将TodoList这UI组件包裹,导出一个容器组件
export default connect(mapStateToProps, mapDispatchToProps)(TodoList)
import React, {Component} from 'react'
import {connect} from "react-redux"
// class TodoList extends Component {
// render() {
// return (
// <div>
// <div>
// {/*此时就要用this.props*/}
// <input type="text" value={this.props.inputValue} onChange={this.props.handleInputChange}/>
// <button onClick={this.props.handleButtonClick}>提交</button>
// </div>
// <ul>
// {this.props.list.map((item,index)=>(
// <li onClick={this.props.handleDelete.bind(this,index)}>{item}</li>
// ))}
// </ul>
// </div>
// )
// }
// }
const TodoList = (props) => {
const {inputValue,handleButtonClick,handleDelete,list,handleInputChange} = props
return (
<div>
<div>
{/*此时就要用this.props*/}
<input type="text" value={inputValue} onChange={handleInputChange}/>
<button onClick={handleButtonClick}>提交</button>
</div>
<ul>
{list.map((item, index) => (
<li onClick={handleDelete.bind(this, index)}>{item}</li>
))}
</ul>
</div>
)
}
// 做连接,如何做连接,就要写映射规则
// mapStateToProps里面写的是props和store中数据的映射规则
// state就是store里面的数据,回调时候会传递过来
const mapStateToProps = (state) => {
return {
// 这么写就是给组件一个props属性叫inputValue, 并将props属性对应store中的属性
inputValue: state.inputValue,
list: state.list
}
}
// 将store.dispatch映射到props中使用
const mapDispatchToProps = (dispatch) => {
return {
// 这么写就是给组件一个props属性叫changeInputValue,而在这个changeInputValue中可以使用传递过来的dispatch
handleInputChange(e) {
const action = {
type: 'change_input_value',
value: e.target.value
}
dispatch(action)
},
handleButtonClick() {
const action = {
type: 'add_item'
}
dispatch(action)
},
handleDelete(index) {
const action = {
type: 'del_item',
index
}
dispatch(action)
}
}
}
// 导出前将TodoList组件和store做连接
export default connect(mapStateToProps, mapDispatchToProps)(TodoList)
4.Redux学习4----react-redux的更多相关文章
- React之redux学习日志(redux/react-redux/redux-saga)
redux官方中文文档:https://www.redux.org.cn/docs/introduction/CoreConcepts.html react-redux Dome:https://co ...
- Redux学习笔记:Redux简易开发步骤
该文章不介绍Redux基础,也不解释各种乱乱的概念,网上一搜一大堆.只讲使用Redux开发一个功能的步骤,希望可以类我的小白们,拜托它众多概念的毒害,大牛请绕道! 本文实例源代码参考:React-Re ...
- 1.Redux学习1,Redux
Redux流程图如上: Action就是一条命令 Store顾名思义就是存储数据的, Reducers是一个回调函数用于处理数据,它处理完数据会返回给Store存储起来 基本流程就是:组件中用Stor ...
- React+Redux学习笔记:React+Redux简易开发步骤
前言 React+Redux 分为两部分: UI组件:即React组件,也叫用户自定义UI组件,用于渲染DOM 容器组件:即Redux逻辑,处理数据和业务逻辑,支持所有Redux API,参考之前的文 ...
- Redux学习(3) ----- 结合React使用
Redux 和React 进行结合, 就是用React 做UI, 因为Redux中定义了state,并且定义了改变或获取state的方法,完全可以用来进行状态管理,React中就不用保存状态了,它只要 ...
- React Redux学习笔记
React Router React Router 使用教程 Redux中间件middleware [译]深入浅出Redux中间件 Redux学习之一:何为middleware? ES6 ES6新特性 ...
- react/redux组件库、模板、学习教程
开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...
- redux学习
redux学习: 1.应用只有一个store,用于保存整个应用的所有的状态数据信息,即state,一个state对应一个页面的所需信息 注意:他只负责保存state,接收action, 从store. ...
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- react+redux官方实例TODO从最简单的入门(6)-- 完结
通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...
随机推荐
- int main (int argc, const char * argv[0]) 中参数的含义;指针数组和数组指针
恩,有的编译器初始化时候会产生这样的参数 argc是命令行总的参数个数,argv[]是argc个参数,其中第0个参数是程序的全名 1. 几种C++ 常见的参数种类 int main(void); in ...
- go变量
go基础 go变量(静态) package main import "fmt" func main() { //申明变量 var zx int //变量赋值 zx=10 //输出变 ...
- css实现等边六边形
在平时的页面布局中,我们也会经常碰到蜂窝煤类型的模块: 那么我们把他拆开,就是单个的六边形,如何用css去实现一个六边形呢?下面是我用绘图软件绘制的css实现六边形的步骤: 具体的html代码如下: ...
- python-布隆过滤器
在学习redis过程中提到一个缓存击穿的问题, 书中参考的解决方案之一是使用布隆过滤器, 那么就有必要来了解一下什么是布隆过滤器.在参考了许多博客之后, 写个总结记录一下. 一.布隆过滤器简介 什么是 ...
- CSS+HTML实现移动端div左右滑动展示
由于手机屏幕的宽度有限,内容太多移动设备一行装不下的,所以很多移动端网站的导航栏都有左右滑动效果,下面我就用CSS+HTML实现移动端div左右滑动展示. CSS:box设置文本不换行,子元素box1 ...
- 图库网站Unsplash高清原图爬虫【华为云技术分享】
[摘要] 写博客的好工具,快速获得高清图片 在百度图片爬虫小助手里,我开发了一个爬虫,来节约我写博客时搜集图片的时间. 但是,也出现了一些问题,主要有以下几点: 百度图片上的质量参差不齐,大部分图片质 ...
- Quantitative proteomics of Uukuniemi virus-host cell interactions reveals GBF1 as proviral host factor for phleboviruses(乌库涅米病毒-宿主细胞互作的定量蛋白质组学揭示了GBF1是个白蛉病毒的前病毒宿主因子)-解读人:谭亦凡
期刊名:Molecular & Cellular Proteomics 发表时间:(2019年12月) IF:4.828 单位:1德国海德堡大学附属医院2德国汉诺威医科大学3德国亥姆霍茲感染研 ...
- .Net core-邮件发送(同步,异步)底层代码(欢迎留言讨论)
using MailKit.Net.Smtp;using MimeKit;using System;using System.Collections.Generic;using System.IO;u ...
- iOS开发动画(Animation)总结
UIView的,翻转.旋转,偏移,翻页,缩放,取反的动画效果 翻转的动画 //开始动画 [UIView beginAnimations:@"doflip" context:ni ...
- Redis中的Java分布式缓存
为什么在分布式Java应用程序中使用缓存?今天学习了两节优锐课讲解分布式缓存的内容,收获颇多,分享给大家. 在提高应用程序的速度和性能时,每毫秒都是至关重要的.例如,根据Google的一项研究,如果网 ...