I'm trying to make an axios.get call and set a const called movies held in the component state with the data that comes back from that call. However, it appears that the API call is not working. The data from the API call never populates the movies const and in return, another function, which relies on the data from said const keeps coming back undefined, breaking the code. Currently, I have the movies const initialized as an empty array, which I thought could be the problem. I tried changing the code and initializing to both an empty string and null. Neither of those changes produced different results. The movies const is still coming back as an empty array. Can someone spot my mistake?
import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import axios from "axios";
const initialMovie = {
id: '',
title: '',
director: '',
metascore: '',
stars: [],
};
const UpdateMovie = () => {
const [movies, setMovies] = useState([]);
const [specificMovie, setSpecificMovie] = useState(initialMovie);
const { id } = useParams();
useEffect(() => {
axios.get('http://localhost:5000/api/movies')
.then(res => setMovies(res.data))
.catch(err => console.log(err))
}, [])
console.log(movies);
useEffect(() => {
const movieToUpdate = movies.find(m => `${m.id}` === id);
if (movies !== []) {
setSpecificMovie(movieToUpdate);
}
}, [movies, id])
const handleChanges = e => {
e.persist();
let value = e.target.value;
setSpecificMovie({
...specificMovie,
[e.target.name]: value
})
};
return (
<div>
<form>
<input type="text" name="title" className="input" value={movieToUpdate.title} />
<input type="text" name="director" className="input" value={movieToUpdate.director} />
<input type="text" name="metascore" className="input" value={movieToUpdate.metascore} />
<button>Submit</button>
</form>
</div>
)
};
export default UpdateMovie;