如何显示 JSON 文件中的 author.name 数据,因为我在尝试运行时遇到错误我是 react js 新手,因此请指导我如何解决此错误,我还添加了错误页面图像,以便您可以看到
class Details extends Component {
constructor(props){
super(props);
this.state = {
question: [],
};
}
componentDidMount() {
axios.get(`http://trivago-magazine-work-sample-server.s3-website.eu-central-1.amazonaws.com/dornoch-castle-scotland-whisky.json`)
.then(res => {
const question = res.data;
console.log(res);
this.setState({ question });
})
}
render() {
const {question} = this.state;
if (question === null) return <p>Loading ...</p>;
return (
<div>
<Container>
<Row >
<h4>{question.author.name}</h4> //This Line when i try to show author.name it showing error
</Row>
</Container>
</div>
)
}
}
export default Details
初始状态应为{ question: null }
,以便if (question === null)
生效并呈现“正在加载...”消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句