[英]React js read file to fetch
I have to read a json file, I followed the following code: stackoverflow我必须阅读 json 文件,我遵循以下代码: stackoverflow
Trying the example shown in the comment, but it's not working for me.尝试评论中显示的示例,但它对我不起作用。
I have to read this file that I have locally, but I'm not succeeding.我必须阅读我在本地拥有的这个文件,但我没有成功。
Can you give me a hand?你能帮我个忙吗?
Link: Codesandbox链接: Codesandbox
Code:代码:
fetch("/user.json")
.then(res => res.json())
.then(res => res.results)
.then(res => {
console.log(res);
let u = res.map((suggestion, i) => ({
label: suggestion.email,
name: suggestion.name.first,
surname: suggestion.name.last,
address: suggestion.location.street.name,
email: suggestion.email,
picture: suggestion.picture.thumbnail
}));
let options =
inputLength === 0
? []
: u.filter(suggestion => {
const keep =
count < 5 &&
suggestion.label &&
suggestion.label.slice(0, inputLength).toLowerCase() ===
inputValue;
if (keep) count += 1;
return keep;
});
this.setState({ options });
})
.catch(e => console.log("Er:", e));
You can just use import or require as follows:您可以使用 import 或 require 如下:
const data = require("./myFile.json")
import data from "./myFile.json"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.