score:0

i don't know if this is exactly what you want:

solution 1: you can make requests on all pages and save them in a state array. every time you make a request on a page, you can make a spread operator on your array and add the new response. but this solution will take a long time as this api is over 1100 pages long.

solution 2: you can draw 5 integer values ​​(between 1 and the number of pages), and, according to the number that comes out, you make the request on the page drawn. with the response to the request, you make another sort and catch the first pokemon and store it in a state array. repeat this operation 4 more times, or as many times as you like. in the end, just render the array with the pokemons drawn!

at first it was these two solutions that came to my mind! if this is in line with your question, good luck!

score:0

generate offset randomly each time, and fetch 5 pokemons it will fetch you 5 consecutive pokemons. you can also fetch all pokemons once and randomly choose 5 indexes. here is sample code. codesandbox https://codesandbox.io/s/gallant-davinci-jfutn?file=/src/app.js:0-946

import { usestate, useeffect } from "react";
import axios from "axios";

function geturl(limit, offset) {
  return `https://pokeapi.co/api/v2/pokemon/?limit=${limit}&offset=${offset}`;
}

export default function app() {
  const [items, setitems] = usestate([]);
  const [offset, setoffset] = usestate(math.floor(math.random() * 1000));

  const generatenewid = () => {
    const id = math.floor(math.random() * 1000);
    setoffset(id);
  };
  // fetch pokemon
  useeffect(() => {
    const fetchitems = async (offset) => {
      const response = await axios.get(geturl(5, offset));
      const data = response.data;
      setitems([...items, ...data.results]);
    };
    fetchitems(offset);
  }, [offset]);

  return (
    <div classname="app">
      <h1>hello codesandbox</h1>
      <h2>start editing to see some magic happen!</h2>
      {items && items.map((item) => <p key={item.url}>{item.name}</p>)}
      <button onclick={generatenewid}>fetch 5 pokemon</button>
    </div>
  );
}

Related Query

More Query from same tag