0

I am trying to implement client side data fetching using swr in nextjs. I am able to display the data fetched using the useSWR hook fine in my desktop browser. I then serve the app on localhost from my desktop and try and test it on my mobile browser over wi-fi (both devices on the same network). The client fetched data is not displayed. Instead the <Error /> component is returned.

What could be possible reason(s) for this? I haven't really seen people bringing up a similar issue on the internet

in useSWRFetch.js

...
const fetcher = (...args) => axios.get(...args).then(res => res.data)

export const useSWRFetch = (url) => {
  const { data, error } = useSWR(url, fetcher)

  return { data, error }
}

in ClientFetchedComponent.js

...
export default function ClientFetchedComponent() {
  const { data, error } = useSWRFetch(`http://localhost:8000/api/category/featured`)

  if (error) return <Error />
  
  return <div>
    { !data ? 
      <Loading /> :
      data.results.map(item => {
        return <div key={item.id}>
          ...
          ...
        </div>
      })
    }
  </div>
}
dEBA M
  • 457
  • 5
  • 19

1 Answers1

1

localhost:8000 is located on the PC, mobile has its own localhost as well that has no apps running.

If you want to use another device with same WIFI replace localhost:8000 with IPv4 Address. ex(192.168.1.4:8000)

Find it on cmd > ipconfig

Chemi Adel
  • 1,964
  • 1
  • 10
  • 18