Skip to content

Instantly share code, notes, and snippets.

@mDibyo
Created December 22, 2018 08:00
Show Gist options
  • Save mDibyo/1d994fb8e3288aafe7bf5bf2e2d7892a to your computer and use it in GitHub Desktop.
Save mDibyo/1d994fb8e3288aafe7bf5bf2e2d7892a to your computer and use it in GitHub Desktop.
class WindowInfo extends React.PureComponent {
render() {
const WithWindowWidth = withHook(() => {
const [width, setWidth] = useState(window.innerWidth);
const handleWindowResize = () => setWidth(window.innerWidth);
useEffect(() => {
window.addEventListener('resize', handleWindowResize);
return () => window.removeEventListener('resize', handleWindowResize);
});
return width;
});
return (
<WithWindowWidth>
{width => <div>The window width is {width}</div>}
</WithWindowWidth>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment