Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda