我正在使用 ReactJS 构建一个网站,该网站使用我计算机本地存在的 JSON 来获取要填写的信息。这是index.js 文件:-
import React from 'react';
import data from 'notes.json';
class BlogList extends React.Component {
constructor(props){
super(props);
this.state={
items:[],
loaded: false
};
}
/* useEffect()=>{
return function(){
}
},[deep])
*/ //Similar to ComponentDidUnMount();
componentDidMount(){
fetch(data)
.then(response => response.json())
.then(json => {
this.setState({
items: json,
loaded: true,
})
})
}
render() {
const {items, loaded} = this.state;
if(!loaded){
return <h3>Loading........</h3>
}
return (
<div className="content-container" >
{
items.map((item)=>(
<div key={item.id}>
<p>
{item.postId}
</p>
<p>
{item.id}
</p>
<p> …Run Code Online (Sandbox Code Playgroud)