I am trying to populate Data from my API into my react form. When I console.log my get statement. I can see the array with the information which means my get is getting the information.
.
Now the issue is trying to use this array that I get back to populate fields- without specifying the array number.
'use client'
import {useEffect, useState } from 'react';
import Axios from "axios";
export default function Page() {
const [user, setUser] = useState([])
const fetchData = () => { Axios.get('http://localhost:5000/api/v1/users', {
headers: {
'Content-Type': 'application/json'
},
}). then((res) =>
{
setUser(res.data)
console.log(res.data)
});
}
return (
<div className='tutorial'>
<h1 className='mb-4 text-2xl'>Data Fetching in React</h1>
<button onClick={fetchData}>Search</button>
<h2> Base: {JSON.stringify(user)}</h2>
</div>
)
I am going to need the information inside of the array so i can for example. Populate a dropdown or populate a name field by user from the API.
I have tried mapping and keying but I was not able to get what I needed. I also did try to
headers: {'Content-Type': 'application/json'},
I feel like I am close but I just cannot get my data to show up and use it without specifying the array number. Example: Data[0] or Data[1]. I want all my data back.
I want to get to an end goal where I can say for a field I want data.firstName and when I hit the search button I can get the firstname for either user.
axios.get(in small cap) My question is do you want to show the response in<h2>tag?useEffectand inside of it call thefetchUserfunction. Then domapover theuser (which you declare in useState)useEffect(() => { fetchData },[])useEffect(() => { fetchData() },[])