I'm calling my function every 10 seconds.
setInterval(this.getStudent, 10000);
Within the function it should grab the latest state of the students array.
getStudent() {
const initialArr = this.state.students;
const student = createStudent(); // this function works
// it just returns a student object
initialArr.push(student);
this.setState({ students: initialArr });
}
If I console.log(this.state.students);
it shows the creation of a student after 10 seconds just fine.
Object // After 10 seconds perfect!
name: Liam Smith
major: BIO
...
But after another 10 seconds (20 seconds total) it should just append one more newly created student. But it attaches an extra so looks like this:
[Object, Object, Object]
And then from there the timer gets all messed up and adds new students whenever it wants. But why is a react state causing this? How can I just simply add a student every 10 seconds?
Ps: I'm calling setInterval within render like so...
render() {
setInterval(this.getStudent, 10000);
console.log(this.state.cards);
return (
<div>
....