Skip to content

Commit 0daffd7

Browse files
author
Dabit
committed
Merge branch 'use-layout' of https://github.com/DylanVann/react-native-hooks into DylanVann-use-layout
2 parents 1fe0e19 + 1840905 commit 0daffd7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-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+
```

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)