API 调用后填充 reaxt ag 网格

Populating reaxt ag grid after API call

我是新来的反应并停留在这一点上。 我正在执行异步 API 调用操作调度程序以获取患者列表 data.Once 返回数据我需要在 ag 网格中呈现它。

这是我的代码:

import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import { fetchPatientsStartAsync } from '../../redux/patients/patients.actions';
import { PatientContainer } from './patients.styles';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-balham.css';

const PatientsComponent = ({ patients, isFetching, fetchPatientsStartAsync }) => {

const [columnDefs, setColumnDefs] = useState([]);

const [rowData, setRowData] = useState([]);

useEffect(() => {
  fetchPatientsStartAsync();
  if (!isFetching && patients) {
  setColumnDefs([
    { headerName: 'UID', field: 'UID'},
    { headerName: 'Name', field: 'name'},
    { headerName: 'Cataract Status', field: 'cataractStatus'},
    { headerName: 'Retina Status', field: 'retinaStatus'},
  ]);
  setRowData(patients);
}
}, [fetchPatientsStartAsync]);

if (!patients ) {
return (
    <>
      <h4>No Patient data for today</h4>
    </>
  )
}
return (
  <>
    <PatientContainer>
      <h1>Patient's Page</h1>
      <div>
        <div className="ag-theme-balham" style={ {height: '50vh'} }>
          <AgGridReact
              columnDefs={columnDefs}
              rowData={rowData}>
          </AgGridReact>
        </div>
      </div>
    </PatientContainer>
  </>
)
};

const mapStateToProps = state => ({
  patients: state.patients.patients,
  isFetching: state.patients.isFetching
});

 export default connect(mapStateToProps, { fetchPatientsStartAsync })(PatientsComponent);

这是我在 patients.actions.js:

中的 fetchPatientsStartAsync()
export const fetchPatientsStartAsync = () => {
return async dispatch => {
dispatch(fetchPatientStart());
  try {
    const patients = await fetchPatients.fetchPatients();
    console.log('Fetched Patients: ', patients);
    dispatch(fetchPatientsSuccess(patients.data.patients));
  } catch (e) {
      console.log('Error: ', e);
      dispatch(fetchPatientsFailed());
    }
  }
 };

所以在我的组件中,fetchPatientsStartAsync 是 运行 异步的,无法在我的 ag 网格中呈现数据。有没有办法在这里设置回调或 Promise,这样一旦 fetchPatientsStartAsync() 完成,我就可以做进一步的 UI 渲染。我也在使用 redux-thunk

您不需要将患者置于 rowData 状态,因为道具将通过 redux 状态更新。如我所见,您的 columnDef 不是动态的,因此您也可以为此创建一个常量。

const columnDefs = [
    { headerName: 'UID', field: 'UID'},
    { headerName: 'Name', field: 'name'},
    { headerName: 'Cataract Status', field: 'cataractStatus'},
    { headerName: 'Retina Status', field: 'retinaStatus'},
]

const PatientsComponent = ({ patients, isFetching, fetchPatientsStartAsync }) => {
  useEffect(() => {
    fetchPatientsStartAsync();
  }, [fetchPatientsStartAsync]);

  if (!patients ) {
    return <h4>No Patient data for today</h4>
  }
  return (
    <>
      <PatientContainer>
        <h1>Patient's Page</h1>
        <div>
          <div className="ag-theme-balham" style={ {height: '50vh'} }>
            <AgGridReact
              columnDefs={columnDefs}
              rowData={patients}>
            </AgGridReact>
          </div>
        </div>
      </PatientContainer>
    </>
  )
};

好的,我的问题已解决。问题在于样式导入。 而不是:

import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-balham.css';

我用过这个:(问题已解决)

import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";