简体   繁体   English

如何延迟 React.js 中的返回以从 JSON 加载数据

[英]How to delay return in React.js for load data from JSON

So far, I have tried several methods that I found on the internet, but none has worked.到目前为止,我已经尝试了几种我在互联网上找到的方法,但没有一个有效。 I tried setTimeout and setting isLoading like in code right now.我现在尝试了 setTimeout 并像在代码中一样设置 isLoading 。

As for loading the data, I am sure that they appear correctly because after deleting loading the brand in the paragraph, the data is visible in the console (more precisely in the React 'Components' add-on)至于加载数据,我确信它们显示正确,因为在段落中删除加载品牌后,数据在控制台中可见(更准确地说是在 React 'Components' 附加组件中)

This is what a JSON file looks like:这是 JSON 文件的样子:

[
   {
    id: 0,
    brand: "Lorem0",
    model: "Lorem",
    dealer: "Lorem",
    main_image: "Lorem",
    sections: [
        {
            id: 0,
            section_type: "",
            section_title: "Lorem",
            section_text: "Lorem Lorem Lorem Lorem Lorem",
            image_left: "Lorem1.png",
            image_center: null,
            image_right: null
        }
      ]
   },
   {
    id: 1,
    brand: "Lorem0",
    model: "Lorem",
    dealer: "Lorem",
    main_image: "Lorem",
    sections: [
        {
            id: 0,
            section_type: "",
            section_title: "Lorem",
            section_text: "Lorem Lorem Lorem Lorem Lorem",
            image_left: "Lorem1.png",
            image_center: null,
            image_right: null
        }
      ]
   },    
]

And this is the code from App.js:这是来自 App.js 的代码:

state = {
   isLoading: true,
}
carData = [];

componentDidMount() {
   fetch("/data.json")
     .then(response => response.json())
     .then(data => {
       this.setState({
         carData: data,
         isLoading: false
       })
     })
 }
 render() {
   return (
     <div className="App">
       {!this.state.isLoading && <p>{this.carData[1].brand}</p>}
     </div>
   );
 }

Maybe I am making a mistake that I cannot see but it seems to me that everything is fine but the error still shows up.也许我犯了一个我看不到的错误,但在我看来一切都很好,但错误仍然出现。

TypeError: Cannot read property 'brand' of undefined TypeError:无法读取未定义的属性“品牌”

Also you can use optional chaining您也可以使用可选链接

this.state.carData[1]?.brand
state = {
   isLoading: true,
   carData: [] // you should put the carData here.
}

componentDidMount() {
   fetch("/data.json")
     .then(response => response.json())
     .then(data => {
       this.setState({
         carData: data,
         isLoading: false
       })
     })
 }
 render() {
   return (
     <div className="App">
       {!this.state.isLoading && <p>{this.state.carData[1].brand}</p>}
     </div>
   );
 }

You are storing data in state but you are not trying to load data from state.您将data存储在 state 中,但您没有尝试从 state 加载数据。

Try something like below:-试试下面的东西: -

state = {
   isLoading: true,
   carData: []  // initialize carData with blank array in state 
}


componentDidMount() {
   fetch("/data.json")
     .then(response => response.json())
     .then(data => {
       this.setState({
         carData: data,
         isLoading: false
       })
     })
 }
 render() {
   return (
     <div className="App">
       {!this.state.isLoading && <p>{this.state.carData[1].brand}</p>}   // use carData with this.state.carData
     </div>
   );
 }

First, add carData as a property to the state object.首先,将carData作为属性添加到 state object。

state = {
   carData: []  // add carData
}

Then you can remove the ìsLoading property if you insert the brand to JSX like this.然后,如果您像这样将brand插入 JSX,则可以删除ìsLoading属性。

render() {
   return (
     <div className="App">
       <p>{this.state.carData[1]?.brand}</p>
     </div>
   );
 }

That ?.那个?. is called Optional Chaining .称为可选链 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM