score:9
Accepted answer
You're spreading an object inside an array, to fix that you should spread the items property inside an array:
function manageList(state = { items: [] }, action) {
switch (action.type) {
case ADD_ITEM:
return { list: [...state.items, action.payload] };
case RESET_LIST:
return {
items: [...state.items, []],
};
default:
return state;
}
}
I think also that you should replace list
and item
by items
:
function manageList(state = { items: [] }, action) {
switch (action.type) {
case ADD_ITEM:
return { items: [...state.items, action.payload] };
case RESET_LIST:
return {
items: [...state.items, []],
};
default:
return state;
}
}
score:1
export const commentReducer = (state = initialState, action) => {
switch (action.type) {
case COMMENT_REQUEST:
return {
...state,
loading: true,
};
case COMMENT_SUCCESS:
return {
...state,
comment: [...state.comment, action.payload],
};
case COMMENT_FAIL:
return { loading: false, error: action.payload };
default:
return state;
}
};
It works for me
score:2
I think you should spread them as "state.items" not as just "state".
Like this:
item: [...state.items,[]]
Not like this:
item: [...state,[]]
Source: stackoverflow.com
Related Query
- TypeError: state is not iterable on react and redux
- React component not updating on final dispatch and update of redux state
- React & Redux - TypeError state is not iteratable
- Dealing with local state in react and redux
- UI not re-rendering on state update using React Hooks and form submission
- No need for state in React components if using Redux and React-Redux?
- Correct way to throttle HTTP calls based on state in redux and react
- React components lifecycle, state and redux
- Props not updating when redux state change in React Hooks
- how and when to call a react component methods after state change from redux
- Sync queryParameters with Redux state and react router for function components
- React Redux Axios: POST Request not receiving credentials from redux state
- How to test redux state update with react testing library and jest
- There is TypeError (0 , _ColorReducer.color) is not a function error in React + Redux code
- React is not updating when Redux state is changed below the first level
- Component does not rerender but redux state has changed via react hooks
- React Router <Link> and <Route> not passing state to Component
- Redux - State is updating but React components are not
- React + Redux: undefined initial state and reducer not called as expected
- React redux not updating global state
- React Native: Redux - How to properly update TextInput via event and have redux state update?
- React keypress event taking only initial state values and not updated values
- React redux - parent state changing, but child components not re-rendering
- React Redux Mapping state to props not working
- Why is redux state not mapped to react state?
- redux and react connect does not work?
- Initial state in React with Redux not working
- React, Redux and Authentication - not finding state
- Changing state but not rerendering react redux
- REACT + REDUX: on redux state change mapStateToProps updating state but view is not rendering as per new state
More Query from same tag
- styled-components typescript error with Material-UI component
- how to use await inside then callback?
- React Hook useEffect has a missing dependency:
- React with Apollo returns process is not defined
- Do we need to do cross browser testing of React application
- How to debounce async formik/yup validation, that it will validate when user will stop entering data?
- displaying array in component populated by Axios get request
- Generate build without hashed file names in react production build
- CRA: Extract particularly big modules into separate chunks?
- How do I embed global React components in Docusaurus v2?
- How to loop through properties of objects inside of an array within a React component?
- React Context State Update
- reducer: adding to array data
- React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes
- React hooks: why does useEffect need an exhaustive array of dependencies?
- How to iteratively add Form.Item in Form.Item in ant design Form
- Creating mixed Bar Chart with ReactJS using recharts or react-chartjs-2
- Using component as a button React
- × TypeError: Cannot read property 'maps' of undefined - google.maps is undefined (React)
- Attempting to render Firebase Auth details returning 'undefined' in React web app
- Count occurrences results from API JSON response in React.js
- Using setFieldValue for one field, based on another filed values
- How can I stop duplicate array in my React - using infinite scroll?
- How to populate a div from the data from fetch
- React Router Link changes URL but doesn't render Component - Rest Countries API
- React query mutation: getting the response from the server with onError callback when the API call fails
- react, express and fetch - send and receive byteArray
- How map date with different API Javascript
- Problem with saving data with react-datetime inside Formik component
- Call methods on React children components