Created
May 10, 2017 09:45
-
-
Save savelichalex/291e5c34a6f64a78611b4d8d15e67f4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React, { Component, PropTypes, cloneElement } from 'react'; | |
| const listeners = {}; | |
| let id = 0; | |
| window.addEventListener( | |
| 'resize', | |
| (event) => { | |
| for (let id in listeners) { | |
| if (listeners.hasOwnProperty(id)) { | |
| let listener = listeners[id]; | |
| listener(event); | |
| } | |
| } | |
| } | |
| ); | |
| class Resize extends Component { | |
| static propsTypes = { | |
| children: PropTypes.node.isRequired, | |
| } | |
| componentWillMount() { | |
| this.id = id++; | |
| listeners[this.id] = (e) => { | |
| this.currentEvent = e; | |
| this.forceUpdate(); | |
| } | |
| } | |
| componentWillUnmount() { | |
| listeners[this.id] = null; | |
| } | |
| render() { | |
| const currentEvent = this.currentEvent; | |
| this.currentEvent = null; | |
| return cloneElement(this.props.children, { | |
| resizeEvent: currentEvent, | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment