Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!