0

I have a react app that is mapping products that i have fetched . However i want the random products to be only 5 and not many.

Here is my code

const Section13: React.FC = () => {
  const [visibleSlides, setVisibleSlides] = useState(6);
  const width = useWindowSize();

  const [productsList, setproductsList] = useState([]);

  useEffect(() => {
    if (width < 370) setVisibleSlides(1);
    else if (width < 650) setVisibleSlides(2);
    else if (width < 950) setVisibleSlides(4);
    else setVisibleSlides(6);

    // get products from DB
    var config: AxiosRequestConfig = {
      method: "get",
      url: "https://backendkwingy.online/api/products",
      headers: {},
    };

    axios(config)
      .then(function (response) {
        setproductsList(response.data.allProducts);
      })
      .catch(function (error) {
        console.log(error);
      });
  }, [width]);

  const productsLists = productsList.sort(
    () => Math.random() - Math.random()
  );

what m mappping is productsList so is there a way i can make the ranom code randomize only 5 products than many

  • Does this answer your question? [How to get a number of random elements from an array?](https://stackoverflow.com/questions/19269545/how-to-get-a-number-of-random-elements-from-an-array) – eol Mar 18 '22 at 08:04
  • no its not answering am failing to understand it. But on mine what i want is randomizing only 5 products than a bunch of products – Collins Jim Mar 18 '22 at 08:11
  • or is there a way i cn limit the number of items on my map function – Collins Jim Mar 18 '22 at 08:13

0 Answers0