Skip to content

Instantly share code, notes, and snippets.

@rscarvalho
Last active March 14, 2017 11:21
Show Gist options
  • Save rscarvalho/b980fc396de735b477ffd5a80c84d52f to your computer and use it in GitHub Desktop.
Save rscarvalho/b980fc396de735b477ffd5a80c84d52f to your computer and use it in GitHub Desktop.
KeyUp handler using RxJS + React
const store = createStore();
ReactDOM.mount(
<Provider store={store}>
<WindowWrapper>
<GameCanvas />
</WindowWrapper>
</Provider>
)
import { keyupAction } from './myactions';
class WindowWrapper extends React.Component {
componentDidMount() {
const { store } = this.context;
this.subscription = Rx.Observable.fromEvent(window, 'keyup')
.filter(event => /* do your filtering here */ true)
.subscribe(event => store.dispatch(keyupAction(event)));
}
componentWillMount() {
this.subscription.unsubscribe();
}
render() {
return React.Children.only(this.props.children);
}
}
WindowWrapper.contextTypes = {
store: React.PropTypes.object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment