Skip to content

Instantly share code, notes, and snippets.

@savelichalex
Created May 10, 2017 09:45
Show Gist options
  • Select an option

  • Save savelichalex/291e5c34a6f64a78611b4d8d15e67f4f to your computer and use it in GitHub Desktop.

Select an option

Save savelichalex/291e5c34a6f64a78611b4d8d15e67f4f to your computer and use it in GitHub Desktop.
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