SWR not returning any data

Viewed 1652

I'm trying to get SWR to work. Every example I have found doesn't seem to work when i apply it to my code. I'm not sure what i'm doing wrong the code appears to be the same, i'm sure something super simple that i just can't see.

I have a boilerplate next.js app.

my index.js has;

import useSWR from 'swr'

export default function Home({ isConnected }) {
  const { data, error } = useSWR('/api/')
  return() //jsx here
}

when i start the development server up it tells me http://localhost:3000 is where the development server can be viewed. when i debug and pause in the on the return line it tells me that data and error are undefined. when i go to http://localhost:3000/api/ i get well formed json back(firefox renders it as json).

1 Answers

You need a method to make the request, for you case, it could be like:

import useSWR from 'swr'
import axios from 'axios';

export default function Home({ isConnected }) {
  const fetcher = async () => {
     return await axios.get('http://mipage/some/');
  };
  const { data, error } = useSWR('/api/', fetcher)
  return() //jsx here
}
Related