Wai Yan Hein
Wai Yan Hein

Reputation: 14831

Unable to mock API request using miragejs in ReactNative Typescript because window is not defined

I am building a mobile application using ReactNative. My app is interacting with an API. I am trying to mock the API requests using Mirage JS when I am building the app locally following this link, https://miragejs.com/quickstarts/react-native/development/. I basically installed miragejs and copied the code from the link into App.tsx file.

This is my code.

if (window.server) {
  server.shutdown()
}

window.server = createServer({
  routes() {
    this.get("/api/movies", () => {
      return {
        movies: [
          { id: 1, name: "Inception", year: 2010 },
          { id: 2, name: "Interstellar", year: 2014 },
          { id: 3, name: "Dunkirk", year: 2017 },
        ],
      }
    })
  },
})

I am getting the following compilation error in the IDE.

enter image description here

How can I fix it?

Upvotes: 0

Views: 562

Answers (0)

Related Questions