当前位置:   article > 正文

React使用react-redux数据共享_react-redux怎么存储共享的变量

react-redux怎么存储共享的变量

下载依赖:

npm i redux

npm i react-redux

npm i redux-thunk

npm i redux-devtools-extension

在src目录下新建文件,如图

 文件说明:

store.js:该文件专门用于暴露一个store对象,整个应用只有一个store对象

constant.js:便于管理,防止程序员单词写错

reducer:变量初始化状态以及同步的逻辑处理

action:页面修改store需要通过action作为媒介

实例说明:求和

 

1.constant.js

  1. /*
  2. 作用:便于管理,防止程序员单词写错
  3. */
  4. export const JIA = 'jia' //
  5. export const JIAN = 'jian' //
  6. export const ASYNC = "async" //异步加

2.actions/count.js

  1. import {JIA,JIAN,ASYNC} from "../constant"
  2. // 同步action:返回的是object
  3. export const jia = data => ({type:JIA,data})
  4. export const jian = data => ({type:JIAN,data})
  5. export const Async = data => ({type:ASYNC,data})
  6. // 异步action:返回的是func
  7. export const AsyncAction = (data,time) => {
  8. return (dispatch) => {
  9. //这里也可以写异步请求
  10. setTimeout(() => {
  11. // 调用同步action
  12. dispatch(Async(data))
  13. },time)
  14. }
  15. }

3. reducer/count.js

  1. import {JIA,ASYNC,JIAN} from "../constant"
  2. /*
  3. 1.该文件是用于创建一个为Count服务的reducer,reducer本质就是一个函数
  4. 2.reducer函数会接到两个参数,
  5. */
  6. // 初始化状态
  7. const count = 0
  8. export default function countAdd(preState = count,action){
  9. // 从action对象中获取:type,data
  10. // preState是旧数据,data是传进来的新数据
  11. const {type,data} = action
  12. switch (type) {
  13. case JIA:
  14. return data + 1
  15. case JIAN:
  16. return data -1
  17. case ASYNC:
  18. return data + 2
  19. default:
  20. return preState
  21. }
  22. }

4.reducer/index.js

  1. /*
  2. 该文件用于汇总所有的reducer
  3. */
  4. import {combineReducers} from "redux"
  5. // 引入Count组件服务的reducer
  6. import count from "./count"
  7. const allReducer = combineReducers({
  8. count
  9. })
  10. export default allReducer

5.store.js

  1. // 该文件专门用于暴露一个store对象,整个应用只有一个store对象
  2. // 引入createStore,用于创建redux中最为核心的store对象
  3. import {createStore,applyMiddleware} from "redux"
  4. // 引入汇总之后的reducer
  5. import allReducer from "./reducer/index"
  6. // 引入redux-thunk,用于异步action
  7. import thunk from "redux-thunk"
  8. import {composeWithDevTools} from "redux-devtools-extension"
  9. // 暴露store
  10. export default createStore(allReducer,composeWithDevTools(applyMiddleware(thunk)))

 6.src/index.js

7.页面使用

  1. import React, { PureComponent } from "react";
  2. import {jia,jian,AsyncAction} from "../../redux/actions/count"
  3. import {connect} from "react-redux"
  4. class Count extends PureComponent {
  5. render() {
  6. return (
  7. <div className="parent">
  8. <h4>当前求和为:{this.props.count}</h4>
  9. <button onClick={this.add}>++</button>
  10. <button onClick={this.jian}>--</button>
  11. <button onClick={this.asyncClick}>异步++</button>
  12. </div>
  13. );
  14. }
  15. add = () => {
  16. let number = this.props.count
  17. this.props.jia(number)
  18. }
  19. jian = () => {
  20. this.props.jian(this.props.count)
  21. }
  22. asyncClick = () => {
  23. let number = this.props.count
  24. this.props.AsyncAction(number,1000)
  25. }
  26. }
  27. export default connect(
  28. state =>({
  29. count:state.count, //state.变量名是在reducer/count文件中声明的变量
  30. }),{
  31. jia,
  32. jian,
  33. AsyncAction
  34. }
  35. )(Count) //上面class的类名

实例说明:添加人

1.constant.js

export const ADD_PERSON = 'person'  //添加人

2.reducer/person.js

  1. import { ADD_PERSON } from "../constant";
  2. // 初始化状态
  3. const person = [
  4. {
  5. name:'lxy',
  6. age:18
  7. }
  8. ]
  9. export default function personReducer(preState = person,action){
  10. const {type,data} = action
  11. switch (type) {
  12. case ADD_PERSON:
  13. return [data,...person]
  14. default:
  15. return preState
  16. }
  17. }

3.reducer/index.js

  1. /*
  2. 该文件用于汇总所有的reducer
  3. */
  4. import {combineReducers} from "redux"
  5. //引入person组件服务reducer
  6. import person from "./person"
  7. const allReducer = combineReducers({
  8. person
  9. })
  10. export default allReducer

 4.actions/person.js

  1. import { ADD_PERSON } from "../constant";
  2. export const Rens = data => ({type:ADD_PERSON,data})

5.页面使用

  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import { Rens } from "../../redux/actions/person";
  4. class Person extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <div>Person组件</div>
  9. <input ref="name" type="text" placeholder="姓名" />
  10. <input ref="age" type="text" placeholder="年龄" />
  11. <button onClick={this.add}>添加</button>
  12. {this.props.personArr.map((item) => {
  13. return <li key={item.name}>name:{item.name},age:{item.age}</li>;
  14. })}
  15. </div>
  16. );
  17. }
  18. add = () => {
  19. const obj = {
  20. name: this.refs.name.value,
  21. age: this.refs.age.value,
  22. };
  23. this.props.Rens(obj)
  24. };
  25. }
  26. export default connect(
  27. (state) => ({
  28. personArr: state.person,
  29. count: state.count,
  30. }),
  31. {
  32. Rens,
  33. }
  34. )(Person);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/510794
推荐阅读
相关标签
  

闽ICP备14008679号