• React 状态管理 - Redux 进阶(上)


    优化使用Redux,让应用更高效

    react中修改全局state就是个同步的过程。

    通过dispatch具体的类型,立刻改变state,和拉取服务端数据的过程是异步的。这个是dispatch没有具体结果

    Redux异步

    Redux Middleware

    • Redux的插件机制,使得Redux默认的同步Action扩展支持异步Action
    1. export function updateName(params) {
    2. return {
    3. type:'UPDATE_NAME',
    4. payload: params
    5. }
    6. }
    7. dispatch(updateName('云'))

     异步

    1. function queryUserInfo(params) {
    2. return (dispatch, getState) {
    3. fetch({
    4. // ...
    5. }).
    6. then((res) => {
    7. dispatch({
    8. type:'FETCH_DATA_SUCCESS',
    9. payload: res.data
    10. })
    11. })
    12. }
    13. }

    1.applyMiddleware接收一系列插件。每个插件(middleware)都会以dispatch和getState作为参数,并返回一个函数。该函数会被传入下一个插件中,直到调用结束。

    1. import {
    2. createStore,
    3. combineReducers, applyMiddleware,
    4. } from 'redux';
    5. // 处理异步的插件
    6. import thunk from 'redux-thunk';
    7. export default createStore(
    8. combineReducers,
    9. applyMiddleware(...[thunk, otherMiddleware])
    10. );

    Reducer起始,插件执行一遍收尾,改变Reducer

    @/src/store/index.js
    1. import { createStore, applyMiddleware } from 'redux';
    2. // 处理异步数据的redux插件
    3. import thunk from 'redux-thunk';
    4. import reducers from '@/reducer';
    5. export default createStore(
    6.   reducers,
    7. applyMiddleware(thunk),
    8. );
     /src/actions/home/index.js
    1. import * as types from '../mutation-types';
    2. // 请求数据方法
    3. export function queryName(params) {
    4.   return {
    5.     type: types.QUERY_GLOBAL_NAME,
    6.     payload: params,
    7.   };
    8. }
    9. // 改变数据方法
    10. export function updateName(params) {
    11.   return {
    12.     type: types.UPDATE_GLOBAL_NAME,
    13.     payload: params,
    14.   };
    15. }
    16. // 异步请求数据
    17. export function queryAsyncName(params) {
    18.   return (dispatch, getState) => {
    19.     setTimeout(() => {
    20. console.log('getState', getState())
    21.       dispatch({
    22.         type: types.QUERY_GLOBAL_NAME,
    23.         payload: params,
    24.       });
    25.     }, 2000);
    26.   };
    27. }
    28. // 异步修改数据
    29. export function asynUpdatecName(params) {
    30.   return async (dispatch) => {
    31.     setTimeout(() => {
    32. // 异步操作中会执行同步操作的方法,只是延迟了。
    33.       dispatch(updateName(params));
    34.     }, 3000);
    35.   };
    36. }
    @/src/containers/home/index.jsx
    1. /* eslint-disable react/no-unused-prop-types */
    2. import React, { Component } from 'react';
    3. // react 的类型检测
    4. import PropTypes from 'prop-types';
    5. import { updateName, queryAsyncName, asynUpdatecName } from '@/actions/home';
    6. import { connect } from 'react-redux';
    7. @connect(
    8.   (state) => state.homeReducer,
    9.   (dispatch) => ({
    10.     updateName: (params) => dispatch(updateName(params)),
    11.     queryAsyncName: (params) => dispatch(queryAsyncName(params)),
    12.     asynUpdatecName: (params) => dispatch(asynUpdatecName(params)),
    13.   })
    14. )
    15. export default class Home extends Component {
    16.   handleClick = () => {
    17.     const {
    18.       // updateName,
    19.       // queryAsyncName,
    20.       asynUpdatecName,
    21.     } = this.props;
    22.     // console.log(updateName);
    23.     asynUpdatecName('异步云');
    24.   };
    25.   render() {
    26.     const { homeName } = this.props;
    27.     return (
    28.       <div>
    29.         <div>{homeName}div>
    30.         <button type='button' onClick={this.handleClick}>
    31.           更改
    32.         button>
    33.       div>
    34.     );
    35.   }
    36. }
    37. // react 的类型定义,类型声明,检测非常重要
    38. Home.propTypes = {
    39.   homeName: PropTypes.string,
    40.   updateName: PropTypes.func,
    41.   queryAsyncName: PropTypes.func,
    42.   asynUpdatecName: PropTypes.func,
    43. };
    44. Home.defaultProps = {
    45.   homeName: '',
    46.   updateName: () => ({}),
    47.   queryAsyncName: () => ({}),
    48.   asynUpdatecName: () => ({}),
    49. };

  • 相关阅读:
    用于单细胞多组学整合的无监督拓扑对齐方法
    seurat dotplot lengend text ptsize
    力扣:133. 克隆图(Python3)
    大一学生想自学web安全
    Redis - Set 集合
    ECCV2022 | 多模态融合检测新范式!基于概率集成实现多模态目标检测
    1、Tomcat整体架构
    【Java基础】数组
    学会这个Python技能,就可以跟excel说再见了
    【Python实战因果推断】2_因果效应异质性2
  • 原文地址:https://blog.csdn.net/qq_35729091/article/details/132605214