Search code examples
javascriptreactjsreduximmer.js

Immer does not support setting non-numeric properties on arrays


I'm trying to update a piece of state with an array of data that I'm getting from the server. This is my reducer:

const schoolsDataReducer = (state = { data: [] }, action) =>
  produce(state, draft => {
    switch (action.type) {
      case SET_INITIAL__DATA:
        draft.data = [...action.payload.data]
        break
    }
  })

I get this error:

"Immer does not support setting non-numeric properties on arrays: data"

How am I supposed to store an array of objects?
Are arrays in the state considered bad practice?
Am I missing something?


Solution

  • This happens when you pass something not an object for state. Make sure state is an object.