I have the following code:
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import axios from "axios";
var config = {
headers: {
Accept: "application/json; odata=verbose",
Authorization: "Bearer " + access_token,
},
};
class Searchbar extends React.Component {
async componentWillMount() {
await axios.get(contracts, config).then((res) => {
this.setState({ allContracts: res.data });
});
}
render() {
return (
<div className="searchbar_wrapper">
<Autocomplete
freeSolo
id="contract-search-bar"
disableClearable
options={contracts.map((option) => option.name)}
onChange={(event, value) => {
let selectedContractID =(value)
}}
renderInput={(params) => (
<TextField
{...params}
label="Search"
margin="normal"
variant="outlined"
InputProps={{ ...params.InputProps, type: "search" }}
/>
)}
/>
</div>
);
}
}
export default Searchbar;
I am using component will mount as I want this function to first make a call before rendering my component... the axios call will get a result like below:
results = [
{ name: "aaa" },
{ name: "bb" },
{ name: "cccc" },
];
The result would be over 500 entries..
The problem I have is when the component is rendering it keeps saying:
Cannot read property 'allContracts' of null
And the reason for this is because the axios function has not finished getting its results...
Please let me know how I can delay rendering and wait for axios to first get results and then continue