10

I am trying to use fetch in my project but there is an error saying

fetch is not defined

enter image description here

Someone knowing why?

3 Answers 3

10

Is this a code style error? if so, you can disable that line according to your code styling library

If not, what you need to import to make it work is:

import "isomorphic-fetch";
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, the response.data should be response.json() after that it turn out to be code style error. Thank you for your response!
7

It might be better to add it in your .eslintrc config instead since the Fetch API is already included in React Native.

Details: https://github.com/eslint/eslint/issues/4015#issuecomment-301920490

Comments

0

Hey if you are using new JS version ES6 then you can fetch your url request like this:

var params = {key:'value'}

var reqOpts = {}
reqOpts.method = 'POST'
reqOpts.headers['Accept'] = 'application/json'
reqOpts.headers['Content-Type'] = 'application/json'
reqOpts.body = JSON.stringify(params)

var url = 'YOUR_URL'

let response = await fetch(url, reqOpts)

return response.json()
      .then((jsonData) => {
        return jsonData
      })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.