There is a get request in componentDidMount()
componentDidMount() {
var manufacturers = []
this.props.participants.map(participant => {
Get("manufacturer/" + participant.id, "").then(result => {
manufacturers.push(result)
console.log(manufacturers)
});
});
this.setState({
participants: manufacturers
})
}
This console.log(manufacturers) shows that there are objects in the array, but the state of component is empty. How to set it properly after fetching data?
Code of Get() function:
const Config = require('Config')
export async function Get(type, userData) {
let BaseURL = Config.serverUrl;
let result = {};
if(userData)
result = await fetch(BaseURL+type, {
method: 'GET',
headers: new Headers({
'Authorization': 'Bearer ' + userData,
'Content-Type': 'application/json'
})
});
else
result = await fetch(BaseURL+type, {
method: 'GET',
headers: new Headers({
'Content-Type': 'application/json'
})
});
let data = await result.json();
return data;
}