I have a page /favorites in my React application and my Axios.post request to setFavArray is giving me an infinte loop when logging it, it is not in useEffect, when it is, the array is only filled on random refreshes. New to react, any idea why the post or useState would cause this?
function Favorites() {
// console.log(login_State)
const [loginState, setLoginState] = useState("");
// const [favoriteID, setFavoriteID] = useState([]);
// const [favResult, setFavResults] = useState();
const [favArray, setFavArray] = useState();
const [resultsData, setResultsData] = useState([]);
Axios.post("http://localhost:3001/favorites", {
username: loginState
})
.then((response) => {
setFavArray(response)
// console.log("FAV ARRAY: ", favArray)
})
useEffect(() => {
Axios.get("http://localhost:3001/login").then((response) => {
if (response.data.loggedIn === true) {
setLoginState(response.data.user[0].username)
// console.log(loginState)
}
})
// fetch('https://api.spoonacular.com/recipes/644366/information?apiKey=blank')
// .then(response => response.json())
// .then((data) => {
// setResultsData(data)
// // console.log("REAL: ", resultsData)
// });
}, []);
console.log("FAV ARRAY: ", favArray)
console.log("REAL SHIT: ", resultsData)
return (
resultsData.map(item => {
return (
<div className="favorites-wrapper">
<h1> All of your favorites in one place! </h1>
<div className="favorite-card">
<Col className='mt-5' xs={6} md={4}>
<Card className='card-hover'>
<Card.Img fluid className='img' variant="top" src={item.image} />
<Card.Body className='card-body'>
<Card.Title className='title' >{item.title}</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the
bulk of the card's content.
</Card.Text>
</Card.Body>
</Card>
</Col>
</div>
</div>
)
})
)
}
export default Favorites;
I tried moving each call in and out of useEffect and no luck. On my back end I'm, just checking a DB to see what 'favorites' the current username has.