2

I have the following problem...

I want to add an object to an array of objects using redux in my react-native application. I get this error message:

Invalid attempt to spread non-iterable instance


// my action

export function newTaskAction(){
  return{
    type: ADD_NEW_TASK,
    payload: {
      new: { id: '5', task:'test123'}
    }
  }
}



//my reducer

export default function taskReducer(state = initialUserState, action){
    switch (action.type)
    {
        case ADD_NEW_TASK :
            return {
                ...state,
                tasks: [...state.tasks, payload.new]
            }
        default:
            return state
    }




const store = createStore(allReducers, {
 tasks: [
   {id: "1", task: "React Native lernen"},
   {id: "2", task: "Redux lernen"},
   {id: "3", task: "Etwas lesen"},
   {id: "4", task: "Einkaufen gehen"}
 ],
 user: 'Frank'
});
3
  • what does state.tasks holdes? Commented Apr 28, 2019 at 10:58
  • I added the current state to my question Commented Apr 28, 2019 at 11:01
  • How did you resolve it? @maximus26 Commented Jan 16, 2021 at 1:22

4 Answers 4

2

Change this line

[...state.tasks, payload.new] 

to

tasks: [...state.tasks, action.payload.new]
Sign up to request clarification or add additional context in comments.

4 Comments

payload: { new: { id: '5', task:'test123'} } . change it to : payload:{ id: '5', task:'test123'} @maximus26
unfortunately no change
you should remove new after the previous change from tasks: [...state.tasks, action.payload.new]. it will be tasks: [...state.tasks, action.payload] at the end. If returns error, tell us what error this is.
it still says Invalid attempt to spread non-iterable instance
0

Isn't it supposed to be?

return { ...state, tasks: [...state.tasks, action.payload.new] }

missing action

9 Comments

@maximus26 check your initialUserState , probably something wrong in there
its just an empty array...tasks:[]
@maximus26 try putting console log and check action and store objects in the reducer. their content should show what's possibly wrong
try this: return console.log(action) || { ...state, tasks: [...state.tasks, action.payload.new] }
and please check if your other reducers are working well. if you have same problem all over the application, then it's probably related to middleware configuration lines.
|
0

Please use Object.assign

 case Actions.CART_ADD_ITEM: 
        return Object.assign({}, state, { tasks: [...state.tasks,  { id: '5', task:'test123'}] });

Please use the task list in View like this

const mapStateToProps = (state) => ({
tasksList: state.tasksReducer.tasks} 
);


<FlatList
    data={this.props.tasksList}/>

Comments

0

In your initial state, if you have defined tasks: null, then when you try to do a spread operator on [ ...state.tasks ] it will throw an error because it is null. Instead define tasks: []. You didn't post that as part of your code examples, so only guessing based on when I've had that same error.

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.