Skip to content

Instantly share code, notes, and snippets.

@zoharlevin
Created January 3, 2018 07:01
Show Gist options
  • Save zoharlevin/0caea26860a9719a5370bfb09d48cc6d to your computer and use it in GitHub Desktop.
Save zoharlevin/0caea26860a9719a5370bfb09d48cc6d to your computer and use it in GitHub Desktop.
HOCs to measure component position and size with onLayout
import {compose, withState, withProps} from 'recompose';
const withMeasurements = compose(
withState('elementX', 'setX', 0),
withState('elementY', 'setY', 0),
withState('elementWidth', 'setWidth', 0),
withState('elementHeight', 'setHeight', 0)
);
const withOnLayout = withProps(({setX, setY, setWidth, setHeight}) => ({
onLayoutSetMeasurements: event => {
setX(event.nativeEvent.layout.x);
setY(event.nativeEvent.layout.y);
setWidth(event.nativeEvent.layout.width);
setHeight(event.nativeEvent.layout.height);
},
}));
export const withSelfMeasure = compose(withMeasurements, withOnLayout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment