reactjs 我们如何在Redux中合并选择器?

pes8fvy9  于 2023-01-25  发布在  React
关注(0)|答案(2)|浏览(131)

假设我需要来自3个不同归约器的3个数据。
总的来说我可以做这样的事情!!

const data1 = useSelector((state: any) => state?.reducer1);
const data2 = useSelector((state: any) => state?.reducer2);
const data3 = useSelector((state: any) => state?.reducer3);

那么,如何使用单个useSelector实现相同的结果呢

m528fe3b

m528fe3b1#

const dataState = useSelector((state: any) => ({reducer1: state?.reducer1, reducer2: state?.reducer2, reducer3: state?.reducer3}));

const data1 = dataState.reducer1;
const data2 = dataState.reducer2;
const data3 = dataState.reducer3;
6l7fqoea

6l7fqoea2#

您可以使用状态的解构赋值,例如:

const { reducer1, reducer2, reducer3 } = useSelector((state: any) =>
  ({
    reducer1: state?.reducer1,
    reducer2: state?.reducer2,
    reducer3: state?.reducer3
  }));

相关问题