Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Showing Uncaught Error: Actions must be plain objects, when trying to dispatch data to store in redux

This is a common error shown when we mess up the action. But in this case, I am trying to dispatch an action with data in its parameter. Action is receiving the data but the reducer is not able to fetch that. I am using the useDispatch hook in this case. Are there any other ways to send data that I might not know, or should I use the connect() function cause useDispatch has some bugs! I am using node v16.15.1 and npm v8.12.2

I have shortened the code for a better understanding

Component

const data = {...}
dispatch(sendData(data)); // the dispatch function will call onClick of a button

Action

export const sendData(data) => {
    type:ADD_DATA, //ADD_DATA is a constant imported for using as type 
    data
}

Reducer

const dataReducer = (state = [], action) => {
    switch(action.type) {
        case ADD_DATA:
            return {
                ...state,
                action.data
            }
        default:
            return state;
    }
}

combined it with combineReducers() function
store created using configureStore()
the app is wrapped on <Provider store={store}></Provider>

Another Component

const data = useSelector(state=> state.dataReducer);
console.log(data);

In console.log empty state is displayed when rendered the component. But won't update onClick and gives the uncaught error Actions must be plain objects

Stuck with the problem for a long time, and need some helping hand
Thank You...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your sendData action creator is wrong:

export const sendData(data) => {
    type:ADD_DATA, //ADD_DATA is a constant imported for using as type 
    data
}

This isn't actually returning anything. Because you have a => and a {, it's a function body, not an object. So, this is actually:

  • a function body
  • a label named type
  • and a reference to data

instead of an object like {type, data}

You could put parentheses around the curly braces to make it an implicitly returned object instead: => ({type, data}).

However, the better answer here is to switch to using our official Redux Toolkit package to write your Redux logic. Redux Toolkit is the the right way to use Redux today, and RTK's createSlice API will automatically generate action creators for you.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda