I am trying to make a React app that allows a user to select from the list of movies requested from a REST API I created. Once the user selects the movie, it displays the url as listed in the JSON API. I've researched many videos and articles and I'm not getting anywhere. Any help is appreciated to help me achieve what I am trying to do with this website. Attached is the screenshot of my progress so far and my code is below. I access the REST api through json-server --watch [json filename]
Movies.js
export default function Movies() {
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [movies, setMovies] = useState([]);
const url = "http://localhost:8900/movies";[![enter image description here][1]][1]
useEffect(() => {
fetch(url)
.then(res => res.json())
.then(
(result) => {
setIsLoaded(true);
setMovies(result);
},
(error) => {
setIsLoaded(true);
setError(error);
}
)
}, [])
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<div>
<select value={(movies.title)}>
{movies.map(movie => (
<option value={movie.id}>
{movie.title}
</option>
))}
</select>
<ul>
{movies.map(item => (
<li>
<a href={item.url}>{item.title}</a>
</li>
))}
</ul>
</div>
);
}
}
test.json
{
"movies" : [
{
"id": 1,
"title": "Space Jam: A New Legacy",
"url": "https://www.imdb.com/title/tt3554046/"
},
{
"id": 2,
"title": "The War With Grandpa",
"url": "https://www.imdb.com/title/tt4532038/"
},
{
"id": 3,
"title": "Ghostbusters (1984)",
"url": "https://www.imdb.com/title/tt0087332/"
},
{
"id": 4,
"title": "Ghostbusters: Afterlife",
"url": "https://www.imdb.com/title/tt4513678/"
},
{
"id": 5,
"title": "Cruella",
"url": "https://www.imdb.com/title/tt3228774/"
},
{
"id": 6,
"title": "Here Today",
"url": "https://www.imdb.com/title/tt10944596/"
},
{
"id": 7,
"title": "The French Dispatch",
"url": "https://www.imdb.com/title/tt8847712/"
},
{
"id": 8,
"title": "Dazed and Confused",
"url": "https://www.imdb.com/title/tt0106677/"
},
{
"id": 9,
"title": "Together Together",
"url": "https://www.imdb.com/title/tt11285280/"
}
]
}
