2

I want to update the react state using redux but data is not sorting correctly

Original Array

    "sections": [
        {
            "id": 8,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "+96******",
            "type": "phone",
            "url": "tel:",
            "icon": "phone"
        }
     {
            "id": 9,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "[email protected]",
            "type": "email",
            "url": "",
            "icon": "email"
        }
    ]

I am updating the state using this code.

state = { ...state,sections :[ ...state.sections.filter(
                (section) => section.id !== action.payload.section.id
              ) , action.payload.section ]  }
            return state

After updating the array objects are getting reversed

   "sections": [
       {
            "id": 9,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "[email protected]",
            "type": "email",
            "url": "",
            "icon": "email"
        }
        {
            "id": 8,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "+91344******",
            "type": "phone",
            "url": "tel:",
            "icon": "phone"
        }
    ]

How can i stop the array reversing?

9
  • 2
    Can you show your state object and reducer in full? Commented Jan 31, 2022 at 8:18
  • I have updated the code . Please check . I am using filter to update "sections" but ut pushing object in last index.. how can i replace the index ? Commented Jan 31, 2022 at 8:27
  • @InderjitSIngh Normally you use splice for that. It not related to react or redux. It's just plain vanilla JS. Commented Jan 31, 2022 at 8:32
  • @super you don't mutate objects in react reducers. Filter preserves the order of the array so the problem is whatever the heck you are doing here ...state,sections :[ ...state.sections.filter( ? Are you using a spread as an object key here ? I'm surprised this is even valid syntax. Commented Jan 31, 2022 at 8:33
  • Yeah agreed @nlta. It would be good to know what specifically are you trying to achieve? Commented Jan 31, 2022 at 8:35

2 Answers 2

1

If you are simply wanting to update an element at a specific index then just use Array.prototype.map to map the previous array to the next, updating the specific element when it's reached. Array order is maintained.

const nextState = {
  ...state,
  sections: state.sections.map(
    section => section.id === action.payload.section.id
      ? action.payload.section
      : section
  ),
};
return nextState;
Sign up to request clarification or add additional context in comments.

Comments

0

You can use one of 2 :

 // filter
  const state = {
    ...state,
    sections: state.sections.filter(
      (section) => section.id !== action.payload.section.id
    ),
  };
  
  // Update
  const state = {
    ...state,
    sections: state.sections.map((section) =>
      section.id !== action.payload.section.id
        ? { ...section, url: 'new Value' }
        : section
    ),
  };

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.