'fetch' 未定义。 eslint(no-undef) in react-native

Ngu*_* Vũ 4 fetch react-native

我在 react native 中使用 fetch 并有一条消息:'fetch' is not defined.eslint(no-undef)

我的代码:

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

}

我该如何解决?和 console.log 不显示数据

thi*_*ign 13

在 .eslintrc 文件中为浏览器设置环境:

env:
  browser: true
Run Code Online (Sandbox Code Playgroud)


blu*_*nex 8

fetch是来自浏览器环境的全局方法。要静默 eslint 警告,您可以将其放在 eslint 配置文件中。

"globals": {
  "fetch": false
}
Run Code Online (Sandbox Code Playgroud)

这是eslint issue对此答案的参考。