I am doing a API call and fetching the data in the componentDidMount in PatientPage.js. Since the render method calls before the componentDidMount when rendering the PatientList.js I'm getting an error in the PatientList bootstrapTable. The reason is since the data is not been fetched yet, the attribute 'data'(this.props.patientList) in PatientList is null.
PatientPage.js:
class PatientPage extends Component {
static propTypes = {
patientState: PropTypes.object.isRequired,
fetchPatientList: PropTypes.func.isRequired,
};
componentWillMount() {
this.props.fetchPatientList();
}
render() {
return (
<div className="content-wrapper">
<PatientList
patientList={ this.props.patientState.patientList }
/>
</div>
);
}
}
PatientList.js:
import React, { Component, PropTypes } from 'react';
import { dateFormatter } from 'Util';
import {BootstrapTable, TableHeaderColumn} from 'react-bootstrap-table';
class PatientList extends Component {
static propTypes = {
patientList: PropTypes.Object,
};
render() {
const data = this.props.patientList;
return (
<div className="box-body">
<div id="PatientManagementTable" className="box-body firstRowHdnTbl table-header-custom">
<BootstrapTable data={ data } striped={ true }>
<TableHeaderColumn dataField='patientId' isKey={ true } dataSort={ true }>Patient ID</TableHeaderColumn>
<TableHeaderColumn dataField='patientName' dataSort={ true }>Patient Name</TableHeaderColumn>
<TableHeaderColumn dataField='patientType' dataSort={ true }>Patient Type</TableHeaderColumn>
</BootstrapTable>
</div>
</div>
);
}
}
export default PatientList;