Skip to content

Commit 1840905

Browse files
committed
add useLayout hook
1 parent cd2e45b commit 1840905

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
@@ -132,3 +132,16 @@ const orientation = useDeviceOrientation()
132132
console.log('is orientation portrait: ', orientation.portrait)
133133
console.log('is orientation landscape: ', orientation.landscape)
134134
```
135+
136+
### `useLayout`
137+
138+
```js
139+
import { useLayout } from 'react-native-hooks'
140+
141+
const {x, y, width, height, onLayout} = useLayout()
142+
143+
<View onLayout={onLayout}>
144+
<View style={{width: width, height: width, backgroundColor: 'red'}} />
145+
<View style={{width: width, height: width, backgroundColor: 'green'}} />
146+
</View>
147+
```

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)