score:4
i think that array.prototype.filter would be the most easy and readable solution to remove an item from an array:
return {
pros: state.filter((item, index) => index !== action.index)
}
score:0
instead of splice
ing, just slice
the array before the index, after the index, and return the combined array.
it'd keep the original array untouched
[
...state.pros.slice(0, action.index),
...state.pros.slice(action.index + 1)
]
score:1
i think this should do the trick ( haven't tested )
you need to create a copy of the array, remove the item, and then assign the new array to the state
const initialstate = {
pros: [''],
}
export const rootreducer = (state = initialstate, action) => {
switch (action.type) {
case action_add_pros:
return {
...state.pros,
pros: [...state.pros, action.payload]
}
case action_change_pros:
return update(state, {
pros: {
[action.index]: {
$set: action.payload
}}
});
case action_remove_pros:
// make a copy of the array
const newarr = state.pros
// remove item at index 4
newarr.splice(newarr.indexof(action.payload), 1)
return update(state, {
pros: [...newarr]
})
//this is the old code that is incorrect
/* return update(state, {
pros: {
[action.index]: {
$set: newarr
}}
});/*
return x
default:
}
return state;
};
for more information about the array.splice()
remember: never edit the state directly. the state needs to be immutable
edit: the return method was wrong, please try this one edit: as from your comment, i corrected the newarr also now it should remove the "current" value from the array basically you wan't to edit the copy of the array and then just assign the smaller array
Source: stackoverflow.com
Related Query
- Remove current array element from Redux array
- How to remove an element from an array in ReactJS Redux state, when a condition is true?
- How to remove element from array object use in redux reduce state?
- Why cant I remove element from array in Reactjs with redux
- What is the cleanest way to remove an element from an immutable array in JS?
- Remove item from array in redux
- Delete element from array in redux state using a reducer in createSlice
- Redux, React, MapStateToProps: Retrieve a specific element in an array from the redux state tree
- Updating Redux store to remove something from array doesn't rerender
- How to remove an element from an array that is inside an object in React JS (ES6)
- Remove element of an object from array of objects - react state
- Remove element from a nested array without using Immutability Helper
- How can i remove employees from the array that have endDate property higher than current selected month?
- how to pass the current item to the function so that i can remove it from array in react component?
- Delete an element from array using redux
- Can't remove specific elements from array by index number from dynamic div element in react?
- How to remove an array from another array in redux state javascript
- How to remove an element from an array in a nested object? (lodash)
- Redux reducer failing to remove array element
- Remove element from nested redux state
- ReactJs Redux: how to remove duplicate objects (with duplicate values) from an array when deciding redux state (after map, filter, etc. functions)?
- Remove element from array with onClick and react
- Remove an element from an array of objects in react js
- How to remove specific element from a array or object using ES6 spread operators and rest operators
- How to remove an element from an array dynamically? React.js
- How can I remove an array from my redux store?
- How to remove element from array after uncheck of checkbox
- reactjs Remove element from array in render method
- How to remove an element from array state in React?
- Remove item from redux array by index
More Query from same tag
- TypeError: Cannot read property 'clickNode' of undefined
- Usestate increment by 1 not working in setInterval
- Reactjs - Get Height of a div/image using React Hooks
- Is it possible to run Mongoose inside next.js api?
- useCallback hook returns older version of state
- React google charts are not displaying
- Cannot read property 'location' of undefined for NavLink in ReactJS
- using an href in react doens't direct to the wanted page, and i get an error when i try to use <link>
- React useEffect() only run on first render with dependencies
- TypeError: firebase.analytics is not a function when mocking with jest
- React . How to save into a variable ,and not in console.log()
- React.js how to call child component method without using refs?
- How to Deploy React + Express Application in Heroku?
- Propertional width with constant spaces in responsive design
- How can I update my child component state from the parent class in React?
- Awaiting for a for loop to end to continue function in javascript
- React JS button not showing up
- ReactCrop cropping image with original resolution instead of the changed using CSS
- How to integrate a react frontend and a separate php REST API backend
- Create Antd Tree based on checked Node of another Antd Tree
- How to filter Products in React.js?
- Question about changing the array value before sorting the array
- How to check if specific word present in nested array of objects
- Next.js pages using Link module not loading files with CSS import
- how to store and get access_token in NextJS SSR
- I am not able to delete items in my todo list made with react, please help me with error in my code
- State is one change behind in select menu
- How to enhance the response in apollo client with react only once
- Redux does not call actions properly
- Passing function to button onClick with anonymous arrow function callback vs as a reference in React?