Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'fetch' is not defined.eslint(no-undef) in react-native

I use fetch in react native and have a message : 'fetch' is not defined.eslint(no-undef)

my code:

getData() {
fetch('https://tutorialzine.com/misc/files/example.json', {})
  .then(res => res.json())
  .then((res) => {
    console.log(res);
  }).catch((e) => {
    console.log(e);
  });

}

how can I fix it? and console.log not show data

like image 781
Nguyễn Tuấn Vũ Avatar asked Apr 05 '19 03:04

Nguyễn Tuấn Vũ


Video Answer


2 Answers

Set the environment for a browser inside the .eslintrc file:

env:
  browser: true
like image 61
thisismydesign Avatar answered Sep 25 '22 21:09

thisismydesign


fetch is a global method from browser environment. To silent eslint warning, you can put this in your eslint config file.

"globals": {
  "fetch": false
}

Here is a reference for this answer from eslint issue.

like image 41
bluenex Avatar answered Sep 23 '22 21:09

bluenex