Created
January 3, 2018 07:01
-
-
Save zoharlevin/0caea26860a9719a5370bfb09d48cc6d to your computer and use it in GitHub Desktop.
HOCs to measure component position and size with onLayout
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 {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