I have array of 4 objects. I have function which returns 3 random values from it. My javascript code is working but I want to display those values in JSX.
const postLinks = [
{
link: "/link1",
title: "Title 1",
},
{
link: "/link2",
title: "Title 2",
},
{
link: "/link3",
title: "Title 3",
},
{
link: "/link4",
title: "Title 4",
},
];
const Home = () => {
const threeLinks = () => {
const resLinks = postLinks
.sort(() => Math.random() - Math.random())
.slice(0, 3);
//console.log(resLinks)
const finalRes = resLinks.map((item) => {
return { itemTitle: item.title, itemLink: item.link };
});
console.log(finalRes);
return finalRes;
};
threeLinks();
return(
<div>
{finalRes}
</div>
)
}
I can't directly write JS function inside JSX. And scope for threeLinks function is limited to that function. How can I get finalRes values in JSX.