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

177
Visualizações
Can't delete document in Firestore from React/Redux

I'm building a CRUD application using redux toolkit and firestore, and I cannot figure out how to delete an item from firestore, and I really don't know why the following code isn't working. Here's the relevant code from the slice:

export const recipeSlice = createSlice({
    name: 'recipesSlice',
    initialState: {
        recipes: []
        
    },
    reducers: {
        ADD_RECIPE: (state, action) => {
            state.recipes.push(action.payload)
        },
        DELETE_RECIPE: (state, action) => {
            state.recipes = state.recipes.filter((recipe) => recipe.recipeId !== action.payload.recipeId)
        },
        
    extraReducers: (builder) => {
        builder.addCase(getRecipes.fulfilled,(state, {payload}) => {
            state.recipes = payload
        })
        .addCase(addRecipeFirestore.fulfilled, (state, {payload}) => {
            state.recipes = payload
        })
    }  
})

And here are the two async thunks that work:

export const getRecipes = createAsyncThunk(
    'recipes/getRecipes',
    async () => {
        const snapshot = await getDocs(collection (db, 'recipes'))
        const array = []
        snapshot.forEach((doc) =>{
            array.push(doc.data())
        })
        return array
    }
)

export const addRecipeFirestore = createAsyncThunk(
    'recipes/addRecipe',
    async (newRecipe) => {
        const docRef = await addDoc(collection(db, "recipes"), newRecipe)
        console.log(docRef.data())
        return docRef.data()
    }
)

And here is the one that I cannot, for the life of me make work:


export const deleteRecipe = ({recipeId}) => {
        return async (dispatch) => {
            const docRef = doc(db, "recipes", `${recipeId}`)
            await deleteDoc(docRef)
            dispatch(DELETE_RECIPE({recipeId}))
        }
}

I didn't use createAsyncThunk because it didn't seem to be a good use case, but I could be wrong. The deleteRecipe function will fire the DELETE_RECIPE reducer and remove the item from the redux store, but it will persist in Firestore.

about 4 years ago · Juan Pablo Isaza
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