假设我需要来自3个不同归约器的3个数据。总的来说我可以做这样的事情!!
const data1 = useSelector((state: any) => state?.reducer1); const data2 = useSelector((state: any) => state?.reducer2); const data3 = useSelector((state: any) => state?.reducer3);
那么,如何使用单个useSelector实现相同的结果呢
useSelector
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;
6l7fqoea2#
您可以使用状态的解构赋值,例如:
const { reducer1, reducer2, reducer3 } = useSelector((state: any) => ({ reducer1: state?.reducer1, reducer2: state?.reducer2, reducer3: state?.reducer3 }));
2条答案
按热度按时间m528fe3b1#
6l7fqoea2#
您可以使用状态的解构赋值,例如: