我想调用两个不同的api,并使用setState将其存储在同一状态变量中。然后将状态数据渲染到平面列表中。我正在使用themoviedb api,如果有所作为。例如,请看下面的代码:
注意:我不想用第二个覆盖第一个api调用数据,而是想将第二个api调用的结果附加到存储在变量movie中的第一个api调用后面。
const [movies, setMovies] = useState([]);
const firstURL = https://api.themoviedb.org/3/movie/now_playing?api_key=###&language=en-US&page=1
const firstCall = async () => {
const response = await fetch(firsturl);
const data = await response.json();
setMovies([...movies, ...data.results]);
}
secondURL = https://api.themoviedb.org/3/movie/now_playing?api_key=###&language=en-US&page=2
const secondCall = async () => {
const response = await fetch(secondURL);
const data = await response.json();
setMovies([...movies, ...data.results]);
}