首页 > 解决方案 > React - useEffect 覆盖具有多个数据的道具

问题描述

我在使用 useEffect 覆盖组件中的映射道具时遇到问题。这发生在更新 npm 之后,我现在不知道该怎么做。

我在一个组件中使用了多个 API 问题,并且数据被使用的最后一个函数覆盖

我不知道如何解决这个问题,为什么会这样?有线的想法是,当我反转功能时,最后一个将覆盖所有道具

情况1:

props.fetchAllClients();
props.fetchAllProducts();

返回:props.clientList = props.productList

案例二:

props.fetchAllProducts();
props.fetchAllClients();

返回:props.productList = props.clientList

useEffect(() => {
    const fetchData = async () => {
        props.fetchAllProducts();
        props.fetchAllClients();
    };

    fetchData();
}, []);

const mapStateToProps = state => ({
    clientList: state.Client.list,
    productList: state.Product.list
})

const mapActionsToProps = {
    addRepair: repairActions.create,
    fetchAllClients: clientActions.fetchAll,
    fetchAllProducts: productActions.fetchAll
}
export default connect(mapStateToProps, mapActionsToProps)(AddNewRepair);

编辑:

api

客户端减速器

产品减速机

还原商店

客户行动

产品行动

编辑2:

减速器/index.js

import { combineReducers } from "redux";

import { Product } from "./product";
import { Repair } from "./repair";
import { Client } from "./client";

export const reducers = combineReducers({
    Product, Repair, Client,
})

减速器返回:

const reducers: Reducer<CombinedState<{
    Product: {
        list: any[];
        groupedList: any[];
    };
    Repair: {
        list: any[];
    };
    Client: {
        list: any[];
    };
}>, AnyAction>

标签: javascriptreactjsreduxredux-thunk

解决方案


推荐阅读