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

99
Vistas
React Fetch Request returning data is undefined

enter image description hereI'm trying to update category item in the list. when i send fetch request to the server its returning response, But when i use that data on text input, It's showing data is undefended. How to fix this issue?

[![

 const { register, handleSubmit, watch, formState: { errors } } = useForm();
    const [data, setData] = useState([]);
    const category_id = useParams();

    useEffect(() => {
        axios.get(`api/admin/category/${category_id.id}`)
            .then(res => {
                setData(res.data)
            })
    },[])

    console.log(data)

    function updateCategory() {

    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

]2]2

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

0

try consol logging data.something like data.id or smthn and when you input the data maybe don't do data.data.attributes.name i think you can just do data.attributes.name since you already saved the first data in the state, if this doesn't work show us were you input the data

set data as a dependency in you useEffect to to make useEffect watch for changes in the state

const [data, setData] = useState([])

useEffect(() => {
    async function GetData() {
        var response = await axios.get(`api/admin/category/${category_id.id}`) #changed the link to what you used
        setData(response.data)
    }
    GetData()
}, [])
console.log('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