As I understand, by default, it’s not possible to get random results from wp-json. Maybe this isn’t so bad either, because I would like the results to be cached. But how can I randomize fetched data in Frontity?
This is a component I’m using:
const Team = ({ state, actions, libraries }) => {
// 1. fetch data from the post type
useEffect(() => {
actions.source.fetch("/team/");
});
// 2. get data from frontity state
const data = state.source.get("/team/");
if (data.isReady && !data.isError) {
return (
<>
<h1>Team</h1>
<MemberContainer>
{
data.items.map(({ type, id }, index) => {
const item = state.source[type][id];
// Render one Item component for each one.
return <Member item={item} />;
})
}
</MemberContainer>
</>
);
}
if (data.isError) {
console.log(data);
}
return null;
}
And I tried using something like this, before the return statement:
data.items.sort(() => Math.random() - 0.5);
But when doing that, I get the following error in my Terminal:
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
So I guess that I’m not handling this the way I should, but what would be the correct approach?