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的更多相关文章

  1. React之redux学习日志(redux/react-redux/redux-saga)

    redux官方中文文档:https://www.redux.org.cn/docs/introduction/CoreConcepts.html react-redux Dome:https://co ...

  2. Redux学习笔记:Redux简易开发步骤

    该文章不介绍Redux基础,也不解释各种乱乱的概念,网上一搜一大堆.只讲使用Redux开发一个功能的步骤,希望可以类我的小白们,拜托它众多概念的毒害,大牛请绕道! 本文实例源代码参考:React-Re ...

  3. 1.Redux学习1,Redux

    Redux流程图如上: Action就是一条命令 Store顾名思义就是存储数据的, Reducers是一个回调函数用于处理数据,它处理完数据会返回给Store存储起来 基本流程就是:组件中用Stor ...

  4. React+Redux学习笔记:React+Redux简易开发步骤

    前言 React+Redux 分为两部分: UI组件:即React组件,也叫用户自定义UI组件,用于渲染DOM 容器组件:即Redux逻辑,处理数据和业务逻辑,支持所有Redux API,参考之前的文 ...

  5. Redux学习(3) ----- 结合React使用

    Redux 和React 进行结合, 就是用React 做UI, 因为Redux中定义了state,并且定义了改变或获取state的方法,完全可以用来进行状态管理,React中就不用保存状态了,它只要 ...

  6. React Redux学习笔记

    React Router React Router 使用教程 Redux中间件middleware [译]深入浅出Redux中间件 Redux学习之一:何为middleware? ES6 ES6新特性 ...

  7. react/redux组件库、模板、学习教程

    开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...

  8. redux学习

    redux学习: 1.应用只有一个store,用于保存整个应用的所有的状态数据信息,即state,一个state对应一个页面的所需信息 注意:他只负责保存state,接收action, 从store. ...

  9. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  10. react+redux官方实例TODO从最简单的入门(6)-- 完结

    通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...

随机推荐

  1. 单核苷酸多态性SNP(single nucleotide polymorphism)

    定义 主要指基因组水平上由单个核苷酸的变异所引起的 DNA 序列多态性. 在基因组水平上由单个核苷酸的变异所引起的DNA序列多态性.即:在不同个体的同一条染色体或同一位点的核苷酸序列中,绝大多数核苷酸 ...

  2. thinkPHP5开发智慧软文遇到的分页第二页不显示数据的问题

    在进行结果查询进行分页的时候,发现分页第一页数据正常,第二页没有数据,后面问题一样,这个是因为直接使用了: 如果此处使用如下语句: $lst=NewsModel::order('sendtime de ...

  3. mysql那些事(6) WHERE条件 字符串的引号

    前言:所谓的坑,两个意思,一个是软件本身的bug,一个是使用者常犯的错误. phper在日常开发中经常和mysql打交道.特别是在没有分层的中小应用中,phper开发要关注sql语句的实现. 入正题, ...

  4. MySQL必知必会(Select, Order by子句)

    SELECT prod_name FROM products ORDER BY prod_name; SELECT prod_id, prod_price, prod_name FROM produc ...

  5. Spring Security OAuth2 Demo —— 隐式授权模式(Implicit)

    本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_impilit_pattern.html 写在前面 在文章OAuth 2.0 概念及授权流程梳 ...

  6. 深入理解 Java 方法

    方法(有的人喜欢叫函数)是一段可重用的代码段.

  7. MySQL 表记录查询小练习

    表记录查询小练习 查看岗位是teacher的员工姓名.年龄 查看岗位是teacher且年龄大于26岁的员工姓名.年龄 查看岗位是teacher且薪资在12000-16000范围内的员工姓名.年龄.薪资 ...

  8. 【CSS】305- [译] Web 使用 CSS Shapes 的艺术设计

    %; %; %; %; 0, 0 100%, 100% 100%); %; %; % 0, 0 100%, 100% 100%); %; %; ) p:nth-of-type(1)::before { ...

  9. v-if和v-show 的区别

    区别 1.手段:v-if是通过控制dom节点的存在与否来控制元素的显隐:v-show是通过设置DOM元素的display样式,block为显示,none为隐藏: 2.编译过程:v-if切换有一个局部编 ...

  10. NTFS在openwrt下的挂载问题

    在openwrt上市可以挂载ntfs分区的,但是如果原来如果搞过win,或者异常关机,那么会遇到以下的错误: root@Openwrt:/etc/config# mount -t ntfs -o rw ...