问题
I am new to react and stuck at this point. I am doing an async API call in action dispatcher to fetch a list of patient data.Once the data is returned I need to render it in ag grid.
This is my code:
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);
And this is my fetchPatientsStartAsync() inside patients.actions.js:
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());
}
}
};
So In my component fetchPatientsStartAsync is running asynchronously and am unable to render data in my ag grid. Is there a way to set a callback or Promise here, so that once fetchPatientsStartAsync() is completed I can do further UI rendering. Also am using redux-thunk
回答1:
You don't need to put patients in rowData
state as props are going to updated through redux state. As I can see your columnDefs are not dynamic so you can create a constant for that as well.
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>
</>
)
};
回答2:
ok, my problem is fixed. The issue was with styles import. Instead of:
import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-balham.css';
I used this: (and issue was fixed)
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";
来源:https://stackoverflow.com/questions/59936187/populating-reaxt-ag-grid-after-api-call