clear setinterval after unmounting component

This commit is contained in:
Ian Minoso 2016-12-14 15:04:56 -08:00
parent 1ed3c1444d
commit f0be3013ac

View file

@ -9,6 +9,7 @@ interface IBody {
interface IBodyState {
currentBuild: any;
intervalId: number;
}
/**
@ -26,11 +27,19 @@ class body extends React.Component<IBody, IBodyState> {
constructor(props){
super(props)
this.state = {
currentBuild: []
currentBuild: [],
intervalId: null
};
}
componentDidMount() {
setInterval(() => this.getBuilds(), 1000);
let intervalId: number = setInterval(() => this.getBuilds(), 1000);
this.setState({
currentBuild: this.state.currentBuild,
intervalId: intervalId
});
}
comoponentDidUnmount() {
clearInterval(this.state.intervalId);
}
getBuilds() {
let api: any = this.props.api;
@ -56,7 +65,8 @@ class body extends React.Component<IBody, IBodyState> {
});
this.setState({
currentBuild: builds
currentBuild: builds,
intervalId: this.state.intervalId
});
});
}