React使用Redux的操作是怎样的?
Admin 2022-12-07 群英技术资讯 847 次浏览
在这篇文章中我们来了解一下“React使用Redux的操作是怎样的?”,一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
开始之前需要强调一下,redux和react没有直接的关系,你完全可以在React, Angular, Ember, jQuery, or vanilla JavaScript中使用Redux。
尽管这样说,redux依然是和React库结合的更好,因为他们是通过state函数来描述界面的状态,Redux可以发射状态的更新, 让他们作出相应; 目前redux在react中使用是最多的,所以我们需要将之前编写的redux代码,融入到react当中去。
这里我创建了两个组件:
Home组件:其中会展示当前的counter值,并且有一个+1和+5的按钮;
Profile组件:其中会展示当前的counter值,并且有一个-1和-5的按钮;

首先将结构搭建出来, 然后安装redux库: npm i redux
安装完成后, 安装我们上一篇文章讲解的目录结构, 创建Store文件夹
store/index.js 中创建store
import { createStore } from "redux";
import reducer from "./reducer";
const store = createStore(reducer)
export default store
store/constants.js 中定义变量
export const CHANGE_NUM = "change_num"
store/reducer.js
import { CHANGE_NUM } from "./constants"
const initialState = {
counter: 10
}
export default function reducer(state = initialState, action) {
switch(action.type) {
case CHANGE_NUM:
return {...state, counter: state.counter + action.num}
default:
return state
}
}
store/actionCreators.js
import { CHANGE_NUM } from "./constants"
export const changeNumAction = (num) => ({
type: CHANGE_NUM,
num
})
store中编写完成后, 在Home和Profile页面中使用store中的state, 核心代码主要是两个:
在 componentDidMount 中监听store的变化,当数据发生变化时重新设置 counter;
在发生点击事件时,调用store的dispatch来派发对应的action;
Home组件
import React, { PureComponent } from 'react'
import store from '../store'
import { changeNumAction } from '../store/actionCreators'
export class Home extends PureComponent {
constructor() {
super()
this.state = {
counter: store.getState().counter
}
}
// 核心一: 在componentDidMount中监听store的变化,当数据发生变化时重新设置 counter;
componentDidMount() {
store.subscribe(() => {
const state = store.getState()
this.setState({ counter: state.counter })
})
}
// 核心二: 在发生点击事件时,调用store的dispatch来派发对应的action;
changeNum(num) {
store.dispatch(changeNumAction(num))
}
render() {
const { counter } = this.state
return (
<div>
<h2>Home</h2>
<h2>当前计数: {counter} </h2>
<button onClick={() => this.changeNum(1)}>+1</button>
<button onClick={() => this.changeNum(5)}>+5</button>
</div>
)
}
}
export default Home
Profile组件
import React, { PureComponent } from 'react'
import store from '../store'
import { changeNumAction } from '../store/actionCreators'
export class Profile extends PureComponent {
constructor() {
super()
this.state = {
counter: store.getState().counter
}
}
componentDidMount() {
store.subscribe(() => {
const state = store.getState()
this.setState({ counter: state.counter })
})
}
changeNum(num) {
store.dispatch(changeNumAction(num))
}
render() {
const { counter } = this.state
return (
<div>
<h2>Profile</h2>
<h2>当前计数: {counter}</h2>
<button onClick={() => this.changeNum(-1)}>-1</button>
<button onClick={() => this.changeNum(-5)}>-5</button>
</div>
)
}
}
export default Profile
我们发现Home组件和Profile组件中的代码是大同小异的, 所以这不是我们最终编写的代码, 后面还会对代码进行优化。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在前端开发的时候,我们往往会使用到tab栏切换,例如一些双项选择就可以使用tab栏切换效果来实现。下面我们就看看二选一的tab栏切换是怎么做的?
这篇文章主要介绍的是Vue 组件组织结构及组件注册,为了能在模板中使用,这些组件必须先注册以便 Vue 能够识别。这里有两种组件的注册类型:全局注册和局部注册。至此,我们的组件都只是通过 Vue.component 全局注册的,文章学详细内容,需要的朋友可以参考一下
目录一、安装axios二、配置axios,添加拦截器三、使用axios发送请求附:Vue3 中全局引入 axios总结axios中文网站:axios-http.com/zh/一、安装axiosnpm install axios --save二、配置axios,添加拦截器在src目录下新建一个request文件夹,在里面
这篇文章给大家分享的是用jQuery写一个查看图片的功能的内容,可以实现点击左右查看图片效果等等,小编觉得挺实用的,因此分享给大家做个参考,接下来一起跟随小编看看吧。
相邻兄弟选择器是指在另一个元素之后可以选择的元素,两者具有相同的父元素。如需选择与另一元素紧密相连的元素,且两者具有相同的父元素,则可使用相邻兄弟选择器。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008