Home > OS >  How we can combine the selector in Redux?
How we can combine the selector in Redux?

Time:01-23

Suppose that I want 3 data from 3 different reducers.

In general I can do something like this!!

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

So how can I achieve the same result with single useSelector

CodePudding user response:

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;

CodePudding user response:

You can use the deconstruction assignment for your state such as:

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