Last active
March 14, 2017 11:21
-
-
Save rscarvalho/b980fc396de735b477ffd5a80c84d52f to your computer and use it in GitHub Desktop.
KeyUp handler using RxJS + React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const store = createStore(); | |
ReactDOM.mount( | |
<Provider store={store}> | |
<WindowWrapper> | |
<GameCanvas /> | |
</WindowWrapper> | |
</Provider> | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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