score:12
Accepted answer
Why not simply:
reducer.js
import {
ADD_CITY, REMOVE_CITY
} from '../Constants';
const cityReducer = (state = [], action) => {
switch (action.type) {
case ADD_CITY:
return [
...state,
action.city
]
case REMOVE_CITY:
return state.filter(city => city !== action.city)
default:
return state;
}
}
export default cityReducer;
score:2
Your remove city reducer should look like
case REMOVE_CITY:
return [
...state.filter(city => city !== action.city),
]
Otherwise you're adding all the previous items plus the filtered list.
Source: stackoverflow.com
Related Query
- Remove item from array in redux
- Remove item from redux array by index
- Redux Dispatch - Remove Item from Array / Incorrect array length output
- Redux wont remove item from state array by ID
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React?
- Remove item from an array (useState Hook)
- Remove item from array in React
- Remove item by key/value from Firestore array
- Remove current array element from Redux array
- Why not to use splice with spread operator to remove item from an array in react?
- Updating Redux store to remove something from array doesn't rerender
- how to pass the current item to the function so that i can remove it from array in react component?
- useState remove item from array
- ReactJS - How to remove an item from an array in a functional component?
- Remove item from array if it already exists
- How to remove item from a generic Array in React Typescript?
- How to remove an array from another array in redux state javascript
- Reactjs formik remove item from an array
- React.js remove item from Array in state syntax
- How to remove item from an array through index in React
- Remove selected item from array
- How to update the global state using redux and remove an item from the global state
- React Redux deleting item from array at specific index
- remove item from array when another item is added to array
- ReactJs Redux: how to remove duplicate objects (with duplicate values) from an array when deciding redux state (after map, filter, etc. functions)?
- How to remove an item from array if it's been clicked (and if it already has been previously added to an array)?
- React - Remove item from array using its name / value
- Firebase React: Remove Item (Map) from array in firestore
- How to remove an element from an array in ReactJS Redux state, when a condition is true?
More Query from same tag
- How to use lightgallery with Gatsby/React
- GSAP Draggable with react ES6 actual this
- How to filter nested object property to update a document in Express and MongoDB?
- Spy on Jest mock
- React Component is Not Displaying the Flash Message on First Submit
- React Js - Enzyme test if setState is triggered
- concat two json objects from different api by id in axios get and store
- Take a snapshot from a video-react
- what <Carousel.Item> means?
- How to Show two components for a url in react js
- Same value is updating in all the elements in the array in React
- Loop and render Object array in React Typescript
- IIS Redirect all request on root
- How to remove Next.js chunk
- Can value of input box be set to to empty string on clicking submit button when the input is in a stateless functional component?
- Uncaught TypeError: Cannot read property 'forEach' of undefined
- User react router inside of map
- Why is dispatch function relocating to home page before rendering logout function?
- expect(...).toHaveAttribute is not a function - Why?
- React dropdown function triggers all dropdowns instead of selected menu
- Unexpected end of JSON on GraphQL query with React while no issue with GraphiQL
- How to prevent arrow key selecting behavior in Material UI auto suggest?
- use ref to programmatically fire click event?
- How to check if a property is null in reactjs?
- How do I mock out location from the router when using enzyme
- How to perform a simple Redirect in a ReactJS app
- React Native - reduce render times to optimize performance while using React hooks
- map doesn't return correct array of object
- How to do infinite scroll in UI along with React JS?
- Why toggleSelection and isSelected method are receiving different key parameter?