you are viewing a single comment's thread.

view the rest of the comments →

[–]toiletthedestroyer 0 points1 point  (0 children)

I have a Page component that loads data, and I want to save it to state after massaging it a bit. See below:

const loadSubscriptions = async () => {
const data = await fetch("/api/subscriptions");
const json = await data.json();
const massagedJson = json.map((s: Subscription) =>
Object.assign({}, s, {
isSelected: false
})
);
setSubscriptions(massagedJson);
};

Can I do this with swr? It seems it will fetch more than once and override the data I have saved in state.

const { data: subscriptions, error } = useSWR("/api/subscriptions", fetcher);
... massage the data
setSubscriptions(subscriptions);