I've couldn't find answer on google so I though you guys might help me out.
So I've tried componentWillMount(), componentDidMount() but it appears substring method gets called before I received fetched string.
import React, { Component } from 'react';
import axios from "axios";
class Home extends Component {
constructor(props){
super(props);
this.state = {
reminders: [{}],
test: "test"
};
}
getReminders(){
fetch('/api/reminder/',{method:"GET"}).then(resp => resp.json()).then(data =>
this.setState({reminders:data}));
}
componentWillMount(){
this.getReminders();
};
render() {
return (
<div className="main-table">
{ this.state.reminders.map((x,i) =>
<div key={i} className="table-item">
<div className="item-name">{x.title}</div>
<div className="item-content flex">
<div className="time">{x.from.substr(11,2)} - {x.to}</div>
<div className="location">{x.location}</div>
</div>
</div>) }
</div>
);
}
}
export default Home;
remindersarray to an empty array instead of an array with an empty object.