3

I want to delete an entity from data (which is List inside object someData). I am using fromJS of immutable js in my reducer to keep the state immutable

I tried using updateIn, deleteIn, update, removeIn and whatever I could find on immutable-js. But it didn't work for me. Most probably I am using these functions the wrong way.

import { fromJS, updateIn } from 'immutable';
import * as type from './constants';

export const initialState = fromJS({
  someData: [],
  loading: true,
});

function someReducer(state = initialState, action) {
  switch (action.type) {
    case type.DELETE_SINGLE_ENTITY:
      updateIn(state, ['someData', 'data'], val =>
        val.filter(x => x.id !== action.id),      
      );
      return state;
    default:
      return state;
  }
}

export default someReducer;

//example someData

/*
{
    date: "",
    data: [
        {
            "id": "1",
            "machine_type": "xyz",
            "created_time": "2019-06-18T10:36:60Z",
            ...
        },
        {
            "id": "22",
            "machine_type": "abc",
            "created_time": "2019-06-20T10:36:60Z",
            ...
        },
        {
            "id": "2",
            "machine_type": "kjhkh",
            "created_time": "2019-06-11T12:36:60Z",
            ...
        }
    ]
}
*/

I want to delete an entity matching with the id passed in action. Before deleting the output of state.get('someData') is in the above example. My expected output (when action.id is 2) when I type state.get should be:

{
    date: "",
    data: [
        {
            "id": "1",
            "machine_type": "xyz",
            "created_time": "2019-06-18T10:36:60Z",
            ...
        },
        {
            "id": "22",
            "machine_type": "abc",
            "created_time": "2019-06-20T10:36:60Z",
            ...
        }
    ]
}
2
  • just create a new object spreading out the state then also specify the someData property on the new object with the array returned from someData.filter Commented Aug 24, 2019 at 16:57
  • 1
    @21x37 My problem was little bit different I didn't want to do it by defensive cloning . Anyways thanks for help. Commented Aug 24, 2019 at 18:22

2 Answers 2

1

Finally! Got it! this:

      return updateIn(state, ['someData', 'data'], val =>
        val.filter(x => x.id !== action.id),      
      );

instead of this:

      updateIn(state, ['someData', 'data'], val =>
        val.filter(x => x.id !== action.id),      
      );
      return state;

Previously I thought updateIn would update the state itself but it doesn't it returns the updated object. So just returning the updateIn would be fine.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it using the filter function

const sampleData = [{id: 1},{id: 2},{id: 3}]

const idToRemove = 2;

const updatedData = sampleData.filter(el => {
  return el.id !== idToRemove
})

console.log(updatedData);

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.