linkedListë gitlab ėŧĪë°
ë°Đëē 1: promise / then ėŽėĐ
useEffect(function () {
fetch(
"https://yts.mx/api/v2/list_movies.json?minimum_rating=9&sort_by=year"
)
.then((response) => response.json())
.then((info) => {
setMovies(info.data.movies);
setLoading(false);
});
}, []);
ë°Đëē 2: async / await ėŽėĐ
const getMovies = async () => {
const json = await (
await fetch(
"https://yts.mx/api/v2/list_movies.json?minimum_rating=9&sort_by=year"
)
).json();
setMovies(json.data.movies);
setLoading(false);
};
useEffect(() => getMovies(), []);
ėīëĨž ë°íėžëĄ ë ëë§í App ėŧīíŽëíļëĨž ėėąíëĪ.
return (
<div>
<h1>Movies {loading ? null : `(${movies.length})`}</h1>
{loading ? (
<strong>ëĄëĐ ėĪ...</strong>
) : (
<div>
{movies.map((movie) => {
return (
<div key={movie.id}>
<a href={movie.url} target="_blank" rel="noreferrer">
<h2>{movie.title}</h2>
</a>
<img src={movie.medium_cover_image} alt={movie.title} />
{movie.genres.map((genre) => (
<li key={genre}>{genre} </li>
))}
<p>{movie.summary}</p>
</div>
);
})}
</div>
)}
</div>
);
ėėëģīęļ° ė―ęē ė ëĶŽíęļ° ėíī js íėžė íë ë ë§ëĪėīė ėŧīíŽëíļëĄ ë§ë í ėīëĨž export & import íëĪ.
import Movie from "./Movie";
import PropTypes from "prop-types";
Movie.propTypes = {
title: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
img: PropTypes.string.isRequired,
genres: PropTypes.arrayOf(PropTypes.string).isRequired,
summary: PropTypes.string.isRequired,
};
// Movie.js
const Movie = ({ title, url, img, genres, summary }) => {
return (
<div>
<a href={url} target="_blank" rel="noreferrer">
<h2>{title}</h2>
</a>
<img src={img} alt={title} />
{genres.map((genre) => (
<li key={genre}>{genre} </li>
))}
<p>{summary}</p>
</div>
);
};
export default Movie;