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 ...
随机推荐
- 个人收藏--未整理—C# http/https 上传下载文件
c# HTTP/HTTPS 文件上传. 分类: .net 2015-02-03 08:36 541人阅读 评论(0) 收藏 举报 方法主体 [csharp] view plaincopy public ...
- Spring(Bean)1
Spring支持3种依赖注入的方式 (DI依赖注入)*属性注入 (配置bean set方法注入) <bean id="car" class="spring.bean ...
- Linux基本架构
Linux linux设计思想 1.程序应该小而专一,程序应该尽量的小,且只专注于一件事上,不要开发那些看起来有用但是90%的情况都用不到的特性: 2.程序不只要考虑性能, 程序的可移植性更重要,sh ...
- PAT(甲级)2017年秋季考试
PAT(甲级)2017年秋季考试 D题红黑树待补21/30 大佬的代码,看着想哭,这才是艺术啊 A Cut Integer 模拟题 #include<bits/stdc++.h> usin ...
- 第二章 Unity Shader基础
[TOC] 1. Unity Shader 的基础: ShaderLab 学习和编写着色器的过程一直是一个学习曲线很陡峭的过程,通常情况下为了自定义渲染效果往往要和很多文件和设置打交道,这些设置很容易 ...
- mysql 替换 tab 键 (\t)
update t_instance set instance_name = replace(instance_name,'\t','') , host_name = replace(host_name ...
- Spring Boot 最简单整合Shiro+JWT方式
简介 目前RESTful大多都采用JWT来做授权校验,在Spring Boot 中可以采用Shiro和JWT来做简单的权限以及认证验证,在和Spring Boot集成的过程中碰到了不少坑.便结合自身以 ...
- 《手把手教你》系列练习篇之9-python+ selenium自动化测试 -番外篇 - 最后一波啊!!!(详细教程)
1. 简介 本来上一篇就是练习篇的最后一篇文章了,但是有的小伙伴私下反映说是做了那么多练习,没有一个比较综合的demo练练手.因此宏哥在这里又补存了一些常见的知识点进行练习,在文章最后也通过实例给小伙 ...
- jQuery基础之事件
jQuery基础之事件方法,如下图: 代码实现: <script src="JS/jquery-1.12.4.min.js"></script> <s ...
- SQL 数字转为中文大写
USE [SPECIAL_BLD]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO CREATE FUNCTION [dbo].[get_upper] ...