I'm working on a React dashboard and I found a library that helps me build a line chart using a json object.
So I'm trying to get 7 objects from my GET requests inside a for loop and fill the data map with them.
This is the json object that I get:
{
"year": "2020-03-03",
"uses": 0
}
I want to have an object like this:
data = [
{
year: 2014,uses: 255,
}, {
year: 2016,uses: 250,
}, {
year: 2018,uses: 540,
},
];
I'm trying to fill the data map inside componentWillMount() like this:
constructor(props) {
super(props);
this.state = {
data ,
};
}
componentWillMount(){
for (let i = 0; i < 7; i++) {
axios.get("http://localhost:5000/transfer/getUsesByDay/"+i).then(
res => {
console.log("http://localhost:5000/transfer/getUsesByDay/"+i)
console.log(res.data)
//TODO : set state => Fill Data
}
)
}
}
componentWillMountis deprecated and not suggested for use. You should usecomponentDidMount.forEachin your code?