在React Native的同一状态变量上存储多个API调用的值

时间:2020-06-08 22:51:31

标签: javascript reactjs react-native themoviedb-api

我想调用两个不同的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]);
    }

0 个答案:

没有答案