keep track of event and stop it when component unmounts
This commit is contained in:
parent
a4bc4a2fdf
commit
64efedb9be
|
@ -14,6 +14,7 @@ class UserTweets extends Component {
|
||||||
user: {},
|
user: {},
|
||||||
tweets: []
|
tweets: []
|
||||||
};
|
};
|
||||||
|
this.event = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount(){
|
componentDidMount(){
|
||||||
|
@ -38,8 +39,7 @@ class UserTweets extends Component {
|
||||||
_subscribeToNewTweetEvent(){
|
_subscribeToNewTweetEvent(){
|
||||||
const username = this.props.match.params.username;
|
const username = this.props.match.params.username;
|
||||||
|
|
||||||
|
this.event = DTwitter.events.NewTweet({_from: web3.utils.keccak256(username), fromBlock: 0})
|
||||||
DTwitter.events.NewTweet({_from: web3.utils.keccak256(username), fromBlock: 0})
|
|
||||||
.on('data', (event) => {
|
.on('data', (event) => {
|
||||||
console.log('new tweet event fired: ' + JSON.stringify(event));
|
console.log('new tweet event fired: ' + JSON.stringify(event));
|
||||||
let index = parseInt(event.returnValues.index);
|
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(){
|
render(){
|
||||||
const {user} = this.state;
|
const {user} = this.state;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue