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

126
Vistas
How to update lists after delete a category

I want to update the Category list after delete a category, I used custom hook for fetching data from the server. I'm not sure how to update state on custom fetch hook

const {data, error, loading} = useFetch("/api/admin/category");
    const [category, setCategory]= useState([]);

    useEffect(() => {
        setCategory(data)
    },[])

    const deleteHandler = (id) => {

        const deleteRequest = async () => {
            const data = await axios.delete(`/api/admin/category/${id}`);
            return data;
        }

        deleteRequest()
            .then(res => {
                data.filter((item) => {
                    return id !== item.id;
                })
            })
    }

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Adding data as the dependency to the useEffect hook may help, try this,

    const {data, error, loading} = useFetch("/api/admin/category");
    const [category, setCategory]= useState([]);

    useEffect(() => {
        setCategory(data)
    },[data])

    const deleteHandler = (id) => {

        const deleteRequest = async () => {
            const data = await axios.delete(`/api/admin/category/${id}`);
            return data;
        }

        deleteRequest()
            .then(res => {
                data.filter((item) => {
                    return id !== item.id;
                })
            })
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

could you add you're custom hook to the code provided? and also be a bit more specific with your query?

import {useEffect, useState} from "react";
import axios from "axios";

const useFetch = (url) => {

    const [data, setData,isLoading] = useState([]);
    const [error, setError] = useState([]);
    const [loading, setLoading] = useState(true);

    useEffect(() => {
        request_get()
        .then(res => {
            if (res.request.status === 200) {
                setTimeout(() => {
                    setLoading(false)
                    setData(res.data.data)
                },1000)
            }
        })

        return () => {
            setData([]);
        }
    }, [])

    const request_get = async () => {
        const data = await axios.get(url)
        return data;
    }

    return {data: data, error: error, loading:loading}

}

export default useFetch;

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar

0

in your custom hook add data to your dependency array;

in your component instead of the folowing

useEffect(() => {
  setCategory(data)
},[])

try to use the spread operator

useEffect(() => {
  setCategory([...data])
},[data])
about 4 years ago · Juan Pablo Isaza Denunciar
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