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

136
Visualizações
Redux Toolkit caseReducers skip prepare callback in reducer function

I have a reducer for adding comment as follow

addComment: {
  reducer: (state,action)=>{
    const { newId, comment } = action.payload
    console.log(newId)
    // functions that create an new comment entry with the newID and update state
  },
  prepare: (input) =>{
    return {
      payload: {
        ...input,
        newId: nanoid(),
      }
  }
}

If I call dispatch within my app as follow, the console.log shows the id generated by nanoid(). So far so good.

dispatch(addComment({comment: comment}))

However if I call the reducer within another reducer using the caseReducers, console.log returns undefined

commentSlice.caseReducers.addComment(state,{
  payload: {
    comment : comment
  }
})

How do I get the prepare callback to run when using caseReducers.

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

0

Based on the docs it looks like if you are to invoke using the caseReducer way, you need to include the "type":

const slice = createSlice({
  name: 'test',
  initialState: 0,
  reducers: {
    increment: (state, action: PayloadAction<number>) => state + action.payload,
  },
})
// now available:
slice.actions.increment(2)
// also available:
slice.caseReducers.increment(0, { type: 'increment', payload: 5 })
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