• React-Redux实现组件间数据共享+项目打包


    目录

    上一个求和案例只用了一个组件,现在看看redux如何实现多个组件之间的数据共享。

    1.步骤

    ①定义一个Person组件,和Count组件通过redux共享数据
    ②为Person组件编写:reducer、action,配置constant常量
    ③使用combineReducers对Count和Person的Reducer进行合并,合并后的总状态是一个对象。
    ④交给store的是总reducer

    2.案例

    /src/containers/Count/index.jsx

    1. import {connect} from 'react-redux'
    2. import React, { Component } from 'react'
    3. // 引入action
    4. import {
    5. increment,
    6. decrement,
    7. incrementAsync
    8. } from '../../redux/actions/count'
    9. // 定义CountUI组件
    10. class Count extends Component {
    11. increment=()=>{
    12. const {value} = this.selectNumer
    13. this.props.increment(value*1)
    14. }
    15. decrement=()=>{
    16. const {value} = this.selectNumer
    17. this.props.decrement(value*1)
    18. }
    19. incrementAsync=()=>{
    20. const {value} = this.selectNumer
    21. this.props.incrementAsync(value*1,500)
    22. }
    23. render() {
    24. return (
    25. <div>
    26. <h1>当前求和为:{this.props.count},下方组件人数:{this.props.personCount}h1>
    27. <select ref={c => this.selectNumer = c}>
    28. <option value="1">1option>
    29. <option value="2">2option>
    30. <option value="3">3option>
    31. select> 
    32. <button onClick={this.increment}>+button> 
    33. <button onClick={this.decrement}>-button> 
    34. <button onClick={this.incrementAsync}>异步加button> 
    35. div>
    36. )
    37. }
    38. }
    39. // 创建并暴露一个Count的容器组件
    40. export default connect(
    41. state => ({count: state.count,personCount:state.persons.length}),
    42. {
    43. increment,
    44. decrement,
    45. incrementAsync
    46. }
    47. )(Count)

    /src/containers/Person/index.jsx

    1. import React, { Component } from 'react'
    2. import { nanoid } from 'nanoid'
    3. import { connect } from 'react-redux';
    4. import { addPerson } from '../../redux/actions/person'
    5. class Person extends Component {
    6. addPerson = () => {
    7. const name = this.nameNode.value;
    8. const age = this.ageNode.value;
    9. const personObj = {id:nanoid(),name,age};
    10. this.props.addPerson(personObj)
    11. }
    12. render() {
    13. return (
    14. <div>
    15. <h1>上方组件的求和为:{this.props.Count}h1>
    16. <input ref={c=>this.nameNode = c} type="text" placeholder='输入名字' /> 
    17. <input ref={c=>this.ageNode = c} type="text" placeholder='年龄' /> 
    18. <button onClick={this.addPerson}>添加button>
    19. <ul>
    20. {
    21. this.props.persons.map((p) => {
    22. return <li key={p.id}>{p.name}--{p.age}li>
    23. })
    24. }
    25. ul>
    26. div>
    27. )
    28. }
    29. }
    30. export default connect(
    31. state => ({persons: state.persons,Count:state.count}),
    32. {
    33. addPerson
    34. }
    35. )(Person)

    /src/redux/actions/count.js

    1. /*
    2. 该文件专门为Count组件生成action对象
    3. */
    4. import { INCREMENT, DECREMENT } from "../constant";
    5. // 同步action,就是指action的值为一般object类型的对象
    6. export const increment = data => ({type:INCREMENT,payload: data})
    7. export const decrement = data => ({type:DECREMENT,payload: data})
    8. // 异步action,就是指action的值为函数
    9. export const incrementAsync = (data, time) => {
    10. return (dispatch)=>{
    11. setTimeout(()=>{
    12. dispatch(increment(data))
    13. },time)
    14. }
    15. }

    /src/redux/reducers/count.js/src/redux/actions/person.js

    1. import {ADD_PERSON} from '../constant'
    2. export const addPerson = personObj => ({type:ADD_PERSON,payload:personObj})

    /src/redux/reducers/index.js

    1. /*
    2. 该文件用于汇总所有reducer为一个总的reducer
    3. */
    4. import {combineReducers} from '@reduxjs/toolkit'
    5. import count from './count' //引入为Count组件服务的reducer
    6. import persons from './person' //引入为Person组件服务的reducer
    7. export default combineReducers({
    8. count,
    9. persons
    10. })

    /src/redux/reducers/count.js

    1. /*
    2. 该文件是用于创建一个为Count组件服务的reducer
    3. */
    4. import { INCREMENT,DECREMENT } from "../constant"
    5. const initState = 0;
    6. export default function countReducer(preState=initState, action) {
    7. const {type, payload} = action;
    8. switch(type) {
    9. case INCREMENT:
    10. return preState+payload;
    11. case DECREMENT:
    12. return preState-payload;
    13. default:
    14. return preState;
    15. }
    16. }

    /src/redux/reducers/person.js

    1. import { ADD_PERSON } from "../constant";
    2. const initState = [{id:'001',name:'tom',age:18}]
    3. export default function personReducer(preState=initState,action) {
    4. const {type,payload} = action
    5. switch(type) {
    6. case ADD_PERSON:
    7. return [payload,...preState]
    8. default:
    9. return preState
    10. }
    11. }

    /src/redux/constant.js

    1. /*
    2. 该模块用于定义action对象中type类型的常量值
    3. */
    4. export const INCREMENT = 'increment'
    5. export const DECREMENT = 'decrement'
    6. export const ADD_PERSON = 'add_person'

    /src/redux/store.js

    1. /*
    2. 该文件用于暴露一个store对象
    3. */
    4. import {configureStore} from '@reduxjs/toolkit'
    5. import reducer from './reducers'
    6. export default configureStore({ reducer: reducer })

    /src/App.jsx

    1. import Count from "./containers/Count"; //引入Count的容器组件
    2. import Person from "./containers/Person" //引入Person的容器组件
    3. function App() {
    4. return (
    5. <div>
    6. <Count/>
    7. <hr />
    8. <Person />
    9. div>
    10. );
    11. }
    12. export default App;

    /src/index.js 

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import App from './App';
    4. import store from './redux/store';
    5. import { Provider } from 'react-redux'; // provider会自动帮容器组件传store
    6. const root = ReactDOM.createRoot(document.getElementById('root'));
    7. root.render(
    8. <Provider store={store}>
    9. <App />
    10. Provider>
    11. );

    效果

    3.打包项目

    全局安装serve库,并配置一下环境变量

    npm i serve -g

    将上面的项目打包,在项目根目录下面执行如下指令,会生成一个build文件夹

    npm run build

    在生产环境运行该项目,serve指令后面跟的build文件夹的路径

    serve build

  • 相关阅读:
    项目实战(一) 瑞吉外卖
    人工神经网络数学模型图,神经网络模型数学建模
    Java ~ Executor ~ ScheduledExecutorService【总结】
    redis作为消息队列的缺点
    CAD安装不正确,用不了
    Java-数据库基本概念
    一、【Photoshop如何根据不同类型图像抠图】
    PAT 甲级 A1107 Social Clusters
    高新企业认定条件
    狄克斯特拉(Dijkstra) 算法 php实现
  • 原文地址:https://blog.csdn.net/YINZHE__/article/details/127621380