简体   繁体   English

在 componentDidMount 中设置 state 时,React 应用程序未显示来自 api 的数据

[英]React app not showing data from api when setting state in componentDidMount

I have some react code where i'm trying to show a list of data in a table.我有一些反应代码,我试图在表格中显示数据列表。 This is the code.这是代码。 The console.log call in the getPagedData displays the correct results in the console but the render is not showing anything. getPagedData 中的 console.log 调用在控制台中显示了正确的结果,但渲染没有显示任何内容。

Seems to be a timing issue with asynchronous call but im stumped.似乎是异步调用的时间问题,但我很难过。 Any ideas why?任何想法为什么?

import React, { Component } from 'react';
import { getSearchResults} from '../services/searchService';
import AircraftsTable from '../components/aircraftsTable';
import Pagination from '../components/common/pagination';
import qs from 'qs';

class SearchResults extends Component {
    state = { 
        aircrafts: [],
        totalCount:0,
        currentPage: 1,
        pageSize: 10
     }

     componentDidMount(){
        const { aircrafts, totalCount} = this.getPagedData();
     
        this.setState(aircrafts, totalCount);
    }

     handlePageChange = page => {
        this.setState({ currentPage: page });
      };

    getPagedData = async () => {
        const queryString = this.getQueryString();
        const searchResults = await getSearchResults(queryString);

        const aircrafts = searchResults.data.aircrafts;
        const totalCount = searchResults.data.totalCount;

        console.log(aircrafts, totalCount);
        
        return [ aircrafts, totalCount ];    
    }
 
    getQueryString(){
        let criteria = this.props.history.location.state?.data;
        criteria.currentPage = this.state.currentPage;
        criteria.pageSize = this.state.pageSize;    
        const criteriaString = qs.stringify(criteria);

        return criteriaString;
    }

    render() { 
        const { aircrafts, totalCount, pageSize, currentPage} = this.state;

        return ( 
            <React.Fragment>
            <AircraftsTable
                aircrafts={aircrafts}
            />
            <Pagination
            itemsCount={totalCount}
            pageSize={pageSize}
            currentPage={currentPage}
            onPageChange={this.handlePageChange}
          />
          </React.Fragment>
         );
    }
}
 
export default SearchResults;

Since you're returning an array and not an object from getPagedData you need to update your componentDidMount to:由于您从getPagedData返回一个数组而不是 object,您需要将您的componentDidMount更新为:

componentDidMount(){
  const [aircrafts, totalCount] = this.getPagedData();
  this.setState({ aircrafts, totalCount });
}

In getPageData function you are returning an array.getPageData function 中,您将返回一个数组。 But you are destructuring an object但是您正在解构 object

return [ aircrafts, totalCount ];

instead of this而不是这个

return { aircrafts, totalCount };

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

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