Home > Net >  Filtered copy array
Filtered copy array

Time:10-27

I made reducer ad my goal i filtered that. Look at my code:

    case 'ACTIVE_USER':
        return {
            ...state,
            users: state.users.filter(user => user.active === 1)
        }
    case 'UNACTIVE_USER':
        return {
            ...state,
            users: state.users.filter(user => user.active === 0)
        }

It works if I run some case. But if I show active users and then I'll try show unactive users it display nothing. I think that I work on this same array not on copy and here is problem. How can I filtered all the time this same array and siplay active on unactive user when I want?

CodePudding user response:

it happened because you override state.users each time you use state.users.filter

so you need to make new array of filteredUsers and make all your computations so as not to affect original users array

CodePudding user response:

Maintain only 'active' state in store.

case 'ACTIVE_USER':
    return {
        ...state,
        active: 1
    }
case 'UNACTIVE_USER':
    return {
        ...state,
        active: 0
    }

In the components

const active = <from store>;
const users = <from store>;
const data = users.filter(user => user.active === active)
  • Related