-
mapStateToProps函数返回的是一个对象
-
返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value
-
mapStateToProps用于传递redux中的状态
function mapStateToProps(state){
return {
count:state
}
}
3.connect的第二个参数mapDispatchToProps
-
mapDispatchToProps函数返回的是一个对象
-
返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value
-
mapDispatchToProps用于传递redux中操作状态的方法
function mapDispatchToProps(dispatch){
return{
increment:(data) => dispatch(incrementAction(data)),
decrement:(data) => dispatch(decrementAction(data)),
incrementAsync :(data,time) => dispatch(incrementActionAsync (data,time)),
}
}
4.index.js入口文件不需要再用store.subscribe进行检测刷新
- react-redux内部可以自己检测到redux状态更改
…
ReactDOM.render(,document…)
/*store.subscribe(() => {
ReactDOM.render(,document…)
}) */
================================================================================
我们接下来就用react-redux去改造上章用redux写的案例,下面是虽然把所有文件全部引入过来,但是改进时基本上只操作了容器组件(这里我把容器组件和其UI组件写在一个文件里,为了方便管理和维护)
效果图:
Count组件 (改动)
-
在文件内加入容器组件,并暴露出去,之前的组件作为其UI组件,
-
容器组件与redux交互,UI组件内部只调用容器组件传入的状态和方法
import React from “react”;
import {connect} from ‘react-redux’
//UI组件
class CountUI extends React.Component{
state = {}
increment = () => {
const {value} = this.selectNumber
this.props.increment(value*1)
}
dencrement= () => {
const {value} = this.selectNumber
this.props.decrement(value*1)
}
incrementIfOdd= () => {
const {value} = this.selectNumber
const {count} = this.props
if(count % 2 !== 0){
this.props.increment(value*1)
}
}
incrementAscyn= () => {
const {value} = this.selectNumber
this.props.incrementAsync(value*1,500)
}
render(){
return(
当前求和:{this.props.count}
<select ref={c => this.selectNumber = c}>
1 2 3
+
-
当前总数为奇数+
异步+
)
}
}
//此时store仓库只关联了Count组件的reducer,所以redux中只有一个状态值,所以state就是count
//后面我们会讲到多个组件共享数据,到时候store会保存多个组件的reducer,我们会通过redux中一个api去合并所有的reducer产生的值到一个对象里
function mapStateToProps(state){
return {
count:state
}
}
function mapDispatchToProps(dispatch){
return{
increment:(data) => dispatch(incrementAction(data)),
decrement:(data) => dispatch(decrementAction(data)),
incrementAsync :(data,time) => dispatch(incrementActionAsync (data,time)),
}
}
//容器组件
export default connect(mapStateToProps,mapDispatchToProps)(CountUI)
store.js(未改动)
/该文件专门用于暴露一个store对象,整个应用只有一个store对象/
import {createStore,applyMiddleware} from “redux”
//引入为Count组件服务的reducer
import reducer from “./reducers”
//引入redux-thunk中间件,用于支持异步action
import thunk from “redux-thunk”
export default createStore(reducer,applyMiddleware(thunk))
constant.js(未改动)
/* 该模块用于定义action对象中type类型的常量值,
便于管理和维护的同时,防止程序员单词写错*/
export const INCREMENT = “increment”
export const DECREMENT = “dencrement”
count_action.js (未改动)
/* 该文件专门为Coun组件生成的action对象;
action的值为对象是同步,为函数是异步*/
import {INCREMENT,DECREMENT} from ‘./constant’
import store from ‘./redux/store’
export const incrementAction = data => ({type:INCREMENT,data})
export const decrementAction = data => ({type:DECREMENT,data})
export const incrementActionAsync = (data,time) => {
//返回一个函数,函数能开启异步任务
return (dispatch) => {
setTimeout(() => {
dispatch(incrementAction(data))
},time)
}
}
count_reducer.js (未改动)
/* 1.该文件用于创建一个为Count组件服务的reducer,reducer的本质就是一个函数
2.reducer函数会传入两个参数:分别为:之前的状态(previousState),动作对象(action)
*/
//redux初始化状态
import {INCREMENT,DECREMENT} from ‘./constant’
const initState = 0 initState = {count:0}
export defaulr function countReducer(preState=initState,action){
const {type,data} = action
switch(type){
case INCREMENT:
return preState + data
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
9pK2sIIe-1714949128862)]
[外链图片转存中…(img-VHl6uVmZ-1714949128862)]
[外链图片转存中…(img-iQ0SXCSe-1714949128862)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!