Next.js Discord

Discord Forum

How can I sequentially display these images in a random order each time the page is refreshed?

Unanswered
Jacquit posted this in #help-forum
Open in Discord
I want the images to appear randomly so that when the user reloads or revisits the page, they will see a different set of images each time.
This is my code:

import React, { useState, useEffect } from 'react';
import Image from '@components/common/atoms/Image';
import useTranslation from 'next-translate/useTranslation';

const AuthLayout = ({ children }: any) => {

const images = [
{
src: 'https://picsum.photos/200/300',
},
{
src: 'https://picsum.photos/id/237/200/300',
},
{
src: 'https://picsum.photos/seed/picsum/200/300',
},
{
src: 'https://picsum.photos/200/300?grayscale',
},
{
src: 'https://picsum.photos/200/300/?blur',
},
{
src: 'https://picsum.photos/id/870/200/300?grayscale&blur=2',
},
]

return (
<div className="relative flex h-screen justify-center md:px-12 lg:px-0">
<div className="mx-auto flex w-full flex-col sm:w-max md:flex-none">
<div className="relative z-10 flex flex-1 flex-col bg-white px-8 pb-16 pt-24 shadow-2xl sm:px-10 md:px-20 lg:px-28">
<div className="mx-auto w-full max-w-md sm:px-4 md:max-w-sm md:px-0">{children}</div>
</div>
</div>
{randomNumber >= 0 && (
<div className="hidden flex-1 sm:contents lg:relative lg:block">
<Image
priority
width={400}
unoptimized
height={600}
src="https://whomoves-assets.b-cdn.net/mountains-f7377c88-f2b3-43fb-9211-dcf12eec63c3.jpg"
alt={t('grey-expansive-green-plain-seen-from-hilltop')}
className="absolute inset-0 h-full w-full object-cover"
/>
</div>
)}
</div>
);
};

export default AuthLayout;

2 Replies

@Jacquit I want the images to appear randomly so that when the user reloads or revisits the page, they will see a different set of images each time. This is my code: import React, { useState, useEffect } from 'react'; import Image from '@components/common/atoms/Image'; import useTranslation from 'next-translate/useTranslation'; const AuthLayout = ({ children }: any) => { const images = [ { src: 'https://picsum.photos/200/300', }, { src: 'https://picsum.photos/id/237/200/300', }, { src: 'https://picsum.photos/seed/picsum/200/300', }, { src: 'https://picsum.photos/200/300?grayscale', }, { src: 'https://picsum.photos/200/300/?blur', }, { src: 'https://picsum.photos/id/870/200/300?grayscale&blur=2', }, ] return ( <div className="relative flex h-screen justify-center md:px-12 lg:px-0"> <div className="mx-auto flex w-full flex-col sm:w-max md:flex-none"> <div className="relative z-10 flex flex-1 flex-col bg-white px-8 pb-16 pt-24 shadow-2xl sm:px-10 md:px-20 lg:px-28"> <div className="mx-auto w-full max-w-md sm:px-4 md:max-w-sm md:px-0">{children}</div> </div> </div> {randomNumber >= 0 && ( <div className="hidden flex-1 sm:contents lg:relative lg:block"> <Image priority width={400} unoptimized height={600} src="https://whomoves-assets.b-cdn.net/mountains-f7377c88-f2b3-43fb-9211-dcf12eec63c3.jpg" alt={t('grey-expansive-green-plain-seen-from-hilltop')} className="absolute inset-0 h-full w-full object-cover" /> </div> )} </div> ); }; export default AuthLayout;
You can use useState to initialize the order of the image array.

One can implement Fisher-Yates shuffle to shuffle the array.
https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

Then do normal looping with map() to iterate through the shuffled array from the getter of the state
Okay, thank you so much @aardani .