Currently I've been pulling data from an API using axios and the useEffect Hook in React. This is done by having the user enter a value into the code field which will be used to make a request to the API. However, when it comes to extracting the data and getting it to display on the page I've been having issues. Normally, I would use data.map() to map the array data to different fields, but when pulling from this API I am getting a project object which I've been having issues mapping to fields for displaying on the application. In addition, I've also noticed that the useEffect hook is constantly being called over and over in the console, and I dunno how to make it be called only once as well. If anyone could help with either of these issues that would be greatly appreciated!
home.js
import React, { useState, useEffect } from 'react';
import axios from "axios";
import './Home.css'; // Import styling
function Home() {
const [data, setData] = useState([])
const [state, setState] = useState({
code: ""
})
const handleChange = e => setState(prevState => ({ ...prevState, [e.target.name]: e.target.value }))
useEffect(() => {
axios.get(baseURL)
.then((response) => {
setData(response.data);
console.log(data)
});
});
return (
<div className="form-container">
<h1>axios-test</h1>
<div className="form-group">
<label>Code</label>
<input type="text" className="form-control" name="code" value={state.code} onChange={handleChange}/>
</div>
<div className="form-group">
<label>Lead</label>
<input type="text" className="form-control" name="Lead" value={data.map(data => data.project.primaryLeaderName)} onChange={handleChange} disabled/>
</div>
</div>
)
}
export default Home;