score:2
Accepted answer
assuming create_post
refers to add_post
based on receiving post data, it would look like this :
case create_post:
return {...state, items: state.items.concat(action.post)};
no need to use object assign since you can just concat
your post with the already existing posts from your state. as a reminder, concat
does not mutate state, it returns a new array with the added post
.
score:1
this is done assuming that a single post inside an array is being passed
function posts(
state = {
items: []
},
action
) {
switch (action.type) {
case receive_posts:
return object.assign({}, state, {
items: action.posts,
})
case create_post:
return object.assign({}, state, {
items: [...state.items, ...action.post]
})
default:
return state
}
}
Source: stackoverflow.com
Related Query
- Updating Redux state partially using object.assing
- Updating and merging state object using React useState() hook
- Updating the array object in React state using immutability helper
- Updating state for a value inside an object immutably in redux reducers
- how do i update state in react for updating the value of an object inside an array using array.map function
- A state is undefined object when using React and Redux , Router
- Updating property of nested object in Redux using action
- Correctly updating Object in React.js State without using spread operator
- How to delete redux state on logout using the history object
- Updating Functional Component Local State Using Data From Redux State
- updating a single object key value inside array of objects react redux state
- Redux state not updating with javascript object
- Redux not updating state correctly -- state stays as empty array despite adding an object in the line prior
- Updating Recursive Object state in Redux
- Redux reducer not updating state object
- Updating a single value for each item in object in Redux State
- Redux saga testing using runSaga not updating the state
- Error while updating redux state using immutability-helper
- React + Redux , error updating state using axios
- How to add new object into existing state using react and redux saga
- Updating state of deep object value in React/Redux using immutable.js
- Async updating multiple properties on a state object using React useState() hook
- How to alter array within an object of a state using redux toolkit
- Redux is not updating component even after returning new state object
- React Redux state not updating when using non mutable methods
- Updating React Component State after Axios Call using Redux
- State not updating when using React state hook within setInterval
- I am using Redux. Should I manage controlled input state in the Redux store or use setState at the component level?
- Redux Form - initialValues not updating with state
- Form input using Redux Form not updating
More Query from same tag
- Can conditional rendering in React use logical operator || when rendering the same content?
- Why has GitLab saved one folder of my project as a Subproject commit?
- Can't access the data of Facebook Graph Api response - reactJs
- Why does "npm install react" install version 16 when latest version is 17?
- Semantic-UI Menu Styling Issue
- Why is onClick being called on render?
- ReactJS Read URL parameters
- State array not populating in React
- React: Using error boundries to highligh broken components
- can't type in input fields when using material-ui-redux-form due to re rendering
- Validation in Yup React based on the value of checkbox
- React map function not rendering
- socket.io messages sent from client aren't received in server
- I want to put Component in TableBody tag
- Communicate between Office taskpane and content addin
- Saving api response to State using useState and Axios (React JS)
- Nested object state setting
- Deploying react app in google cloud
- `react-table` v7 how to preserve a grouping's sort order whilst the rows of the groups are sorted
- Axios won't send the body parameter
- React state update step behind
- Redux Toolkit caseReducers skip prepare callback in reducer function
- Array saving as blank in MongoDB database from React Axios post
- Inputing number between a range in React
- Way to pass value during Button on click to another component with async in react
- Imported image not showing in React
- Consume API with basic authentication using Javascript without exposing key pair
- Ant Design select tag no data rename
- JS/React How to prevent data from being fetched if URL doesn't exist?
- justifyContent with createStyles in TypeScript