Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Fetch data from multiple pages of an API in react native

React.useEffect(() => {
    const getAllGames = async(page: number|null): void => {
      if (Number.isInteger(page)){
        const result = await fetch(apiURL + "/games?page="+page)
        const data = await result.json()
        const { results: games } = data;
        if (data.next) { 
          setTimeout(
            getAllGames(
              parseInt(data.next.charAt(data.next.length-1))), 10000)
        }
        setGames(previousGames => [...games, ...previousGames]);
      }
    }
    getAllGames(1)
  }, []);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Fetch #data #multiple #pages #API #react #native
ADD COMMENT
Topic
Name
1+2 =