0

I have following reducer:

case INIT:
  return {
    ...state,
    items: [...state.items, ...action.payload],
  };

I need somehow to check if in state.items array are items from action.payload array and do not include them in state.items. I have such a code:

[...state.items.filter(item => item !== action.payload)],

but it doesn't what I need, it doesn't work. Can somebody help me?

3
  • 1
    What data types do you reference in state.items and action.payload? Objects or primitive types? Commented Sep 26, 2018 at 7:47
  • it is strings.. Commented Sep 26, 2018 at 7:50
  • [...state.items.filter((item) => item !== action.payload)] what array does your code return for this? Commented Sep 26, 2018 at 7:53

2 Answers 2

2

The easiest way to handle that with primitive types is using Set:

case INIT:
    return {
        ...state,
        items: [...new Set(state.items.concat(action.payload))],
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You should filter the payload.

case INIT:
      const { items } = state;
      const newItems = action.payload.filter(item => !items.includes(item))]
      return {
        ...state,
        items: items.concat(newItems),
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.