简体   繁体   English

如何从 themoviedb api 获取多页结果并存储在 React Native 中的 state 中?

[英]How do I get multiple pages of result from themoviedb api and store in state in React Native?

I am stuck in trying to get page 2 results in the same state... Please take a look at my code and help me.我一直试图在相同的 state 中获得第 2 页的结果...请查看我的代码并帮助我。

    const URL = 'https://api.themoviedb.org/3/movie/now_playing?api_key=#&language=en-US&page=1';
    const [movies, setMovies] = useState([]);

    const testapi = async() => {
      const response = await fetch(urlGiven);
      const data = await response.json();
      setMovies(data.results);
    }

Now I am trying to another request to the api with &page=2 and set that data to append my current movies const... I tried doing setState([...movies, data.results]) again for:现在我正在尝试使用 &page=2 向 api 发出另一个请求,并将该数据设置为 append 我当前的电影常量...我尝试再次执行 setState([...movies, data.results]) :

https://api.themoviedb.org/3/movie/now_playing?api_key=#&language=en-US&page=2

PAGE 2 data but that did not render correctly in react native flatlist. PAGE 2 数据,但在 react native flatlist 中未正确呈现。

For example, like this:例如,像这样:

const secondCALL = async() => {
    const response = await fetch(page2URL);
    const data = await response.json();
    setMovies([...movies, data.results]);
}

How do I go on about appending page 2 data right after page one's original that is set in here: setMovies(data.results);我如何 go 关于在此处设置的原始页面之后附加第 2 页数据:setMovies(data.results);

My flatlist set up, for example ( only showing the relevant part)例如,我的平面列表设置(仅显示相关部分)

<FlatList
            data={movies}
/>

You need to add... at data.results, otherwise you will have [movie1, movie2, [movie3, movi4]]您需要在 data.results 中添加...,否则您将拥有 [movie1, movie2, [movie3, movi4]]

const secondCALL = async() => {
   const response = await fetch(page2URL);
   const data = await response.json();
   setMovies([...movies, ...data.results]);
}

You need to use the spread operator synthax to expands the content of data.results.您需要使用扩展运算符语法来扩展 data.results 的内容。

consider this example:考虑这个例子:

const [movies, setMovies] = useState([]);
const [page, setPage] = useState(1);


const getMovies = async () => {
  try{
    let req = await fetch(`'https://api.themoviedb.org/3/movie/now_playing?api_key=#&language=en-US&page=${page}`);
    let data = await req.json();
    // spread the content of movies and data 
    setMovies([...movies, ...data]);
  }catch(err){
   throw err
  }
}

return (...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在React Native中如何通过相同的状态调用获得不同的结果? - How do I get a different result with the same state call in React Native? 为什么我无法在React Native中使用Mobx从Store获取状态? - Why I cannot get state from Store with Mobx in react native? 如何在themoviedb API中获得所有评论? - How to get all reviews in themoviedb api? 我如何从其他组件中获取this.state.value - How do I get a this.state.value from other components in react native 在React with Redux中从我的商店/状态中删除项目后,如何更新视图 - How do I get my view to update after an item was removed from my store/State in React with Redux 如何使用 react-native 从 API 获取嵌套对象 - How do get nested objects from an API with react-native 我不知道如何在 nodejs express 服务器中存储来自 api 调用的 json 数据,并将其发送到反应本机客户端 - I do not know how to store json data from an api call within a nodejs express server, and send it to a react native client 我如何让本机反应播放多个本地文件 - how do i get react native to play multiple local files 如何从API响应中的多个页面获取结果? - How can I get the results from multiple pages in an API response? 我如何从表单提交的多个输入框中获取值并将这些值作为对象存储在React的状态数组中? - How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM