Basically, I created a little api route in the nextjs directory which returns a list of strings and I want to randomly display them on screen but I am unable to do so... How should I go about solving this?
This is my index.js
import useSWR from 'swr'
export default function Home() {
const{ data, error } = useSWR('./api/stuff')
if (error) {
return <div>Failed to load stuff</div>
}
if (!data) {
return <div>Loading the stuff...</div>
}
function randomizeThestuff(data) {
return data[Math.floor(Math.random() * data.length)];
}
return (
<h1 className="text-3xl font-bold underline">
randomizeThestuff(data)
</h1>
)
}
This is my /api/stuff.js file
const stuff = [
"value",
"value",
"etc",
]
export default function handler(req, res) {
res.status(200).json(
stuff
)
}
and my browser is showing this