keep track of event and stop it when component unmounts

This commit is contained in:
Iuri Matias 2018-07-05 13:56:33 +03:00
parent a4bc4a2fdf
commit 64efedb9be
1 changed files with 9 additions and 3 deletions

View File

@ -14,6 +14,7 @@ class UserTweets extends Component {
user: {},
tweets: []
};
this.event = null;
}
componentDidMount(){
@ -38,8 +39,7 @@ class UserTweets extends Component {
_subscribeToNewTweetEvent(){
const username = this.props.match.params.username;
DTwitter.events.NewTweet({_from: web3.utils.keccak256(username), fromBlock: 0})
this.event = DTwitter.events.NewTweet({_from: web3.utils.keccak256(username), fromBlock: 0})
.on('data', (event) => {
console.log('new tweet event fired: ' + JSON.stringify(event));
let index = parseInt(event.returnValues.index);
@ -62,6 +62,12 @@ class UserTweets extends Component {
});
}
componentWillUnmount() {
if (!this.event) return;
// TODO: check if this is the 'right' way to remove / stop the event listener
this.event.removeListener(this.event);
}
render(){
const {user} = this.state;
@ -98,4 +104,4 @@ class UserTweets extends Component {
}
}
export default UserTweets
export default UserTweets