Skip to content

Commit c962510

Browse files
author
Dabit
committed
Merge branch 'DylanVann-use-layout'
2 parents 1fe0e19 + b97154a commit c962510

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,16 @@ const orientation = useDeviceOrientation()
110110
console.log('is orientation portrait: ', orientation.portrait)
111111
console.log('is orientation landscape: ', orientation.landscape)
112112
```
113+
114+
### `useLayout`
115+
116+
```js
117+
import { useLayout } from 'react-native-hooks'
118+
119+
const {x, y, width, height, onLayout} = useLayout()
120+
121+
<View onLayout={onLayout}>
122+
<View style={{width: width, height: width, backgroundColor: 'red'}} />
123+
<View style={{width: width, height: width, backgroundColor: 'green'}} />
124+
</View>
125+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import useAccessibilityInfo from './lib/useAccessibilityInfo'
66
import useKeyboard from './lib/useKeyboard'
77
import useInteractionManager from './lib/useInteractionManager'
88
import useDeviceOrientation from './lib/useDeviceOrientation'
9+
import useLayout from './lib/useLayout'
910

1011
export {
1112
useDimensions,
@@ -16,4 +17,5 @@ export {
1617
useKeyboard,
1718
useInteractionManager,
1819
useDeviceOrientation,
20+
useLayout
1921
}

lib/useLayout.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function useLayout() {
2+
const [layout, setLayout] = React.useState({
3+
x: 0,
4+
y: 0,
5+
width: 0,
6+
height: 0,
7+
})
8+
const onLayout = React.useCallback(e => setLayout(e.nativeEvent.layout), [])
9+
return {
10+
onLayout,
11+
...layout,
12+
}
13+
}

0 commit comments

Comments
 (0)