dev_el
dev_el

Reputation: 2957

nextjs swr syntax clarification

I'm following a tutorial and see the following code:

import useSWR from "swr"
import { fetcher } from "./fetcher"

export function useFeed() {
  const { data: feed } = useSWR("/api/feed", fetcher)
  return { feed }
}

What's with the : between data and feed? And why is feed being returned instead of data? I see this semicolon pattern repeated in the nextjs swr documentation. Can someone explain to me what it does?

Upvotes: 0

Views: 147

Answers (1)

oozywaters
oozywaters

Reputation: 1241

It has nothing to do with swr library. This pattern is a part of ES6 syntax and is used to rename a variable while destructuring it (for instance, to avoid a variable name duplication, since data is a commonly used name).

You could replace : syntax with the following and have the same result:

export function useFeed() {
  const feed = useSWR("/api/feed", fetcher).data;
  return { feed };
}

Upvotes: 1

Related Questions