Skip to content

Instantly share code, notes, and snippets.

@wuct
Last active December 1, 2015 11:58
Show Gist options
  • Save wuct/cba6e53cad3a17c0a977 to your computer and use it in GitHub Desktop.
Save wuct/cba6e53cad3a17c0a977 to your computer and use it in GitHub Desktop.
add and remove listner
import React from 'react';
import MyStore from './MyStore';
class MyComponent extends React.Component {
componentDidMount() {
MyStore.addChangeListener(this._onChange.bind(this));
}
componentWillUnmount() {
MyStore.removeChangeListener(this._onChange.bind(this)); // not working
MyStore.removeChangeListener(this._onChange); // not working, too
}
render() {
return (
<div></div>
);
}
_onChange(){
this.setState(/* set something */);
}
}
export default MyComponent;
@jchouse
Copy link

jchouse commented Dec 1, 2015

class Tooltip extends React.Component {
    constructor (props) {
        super(props);

        this.state = {
             handleOutsideClick: this.handleOutsideClick.bind(this)
        };
    }

    componentDidMount () {
         window.addEventListener('click', this.state.handleOutsideClick);
    }

    componentWillUnmount () {
         window.removeEventListener('click', this.state.handleOutsideClick);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment