Skip to content

Commit ab18bb8

Browse files
author
Dabit
committed
Merge branch 'master' of github.com:react-native-community/react-native-hooks
2 parents 3272487 + 5cb8be1 commit ab18bb8

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ yarn add react-native-hooks
2121
## API
2222
- [useAccessibilityInfo](https://github.com/react-native-community/react-native-hooks#useaccessibilityinfo)
2323
- [useAppState](https://github.com/react-native-community/react-native-hooks#useappstate)
24+
- [useBackHandler](https://github.com/react-native-community/react-native-hooks#usebackhandler)
2425
- [useCameraRoll](https://github.com/react-native-community/react-native-hooks#usecameraroll)
2526
- [useClipboard](https://github.com/react-native-community/react-native-hooks#useclipboard)
2627
- [useDimensions](https://github.com/react-native-community/react-native-hooks#usedimensions)
@@ -45,6 +46,21 @@ import { useAppState } from 'react-native-hooks'
4546
const currentAppState = useAppState()
4647
```
4748

49+
### `useBackHandler`
50+
51+
```js
52+
import { useBackHandler } from 'react-native-hooks'
53+
54+
useBackHandler(() => {
55+
if (shouldBeHandledHere) {
56+
// handle it
57+
return true
58+
}
59+
// let the default thing happen
60+
return false
61+
})
62+
```
63+
4864
### `useCameraRoll`
4965

5066
```js

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import useDimensions from './lib/useDimensions'
22
import useAppState from './lib/useAppState'
3+
import useBackHandler from './lib/useBackHandler'
34
import useCameraRoll from './lib/useCameraRoll'
45
import useClipboard from './lib/useClipboard'
56
import useAccessibilityInfo from './lib/useAccessibilityInfo'
@@ -11,6 +12,7 @@ import useLayout from './lib/useLayout'
1112
export {
1213
useDimensions,
1314
useAppState,
15+
useBackHandler,
1416
useCameraRoll,
1517
useClipboard,
1618
useAccessibilityInfo,

lib/useAppState.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useEffect, useState } from 'react'
22
import { AppState } from 'react-native'
33

4-
const currentState = AppState.currentState
5-
4+
// https://github.com/facebook/react-native/issues/18836
65
export default () => {
6+
const currentState = AppState.currentState
77
const [appState, setAppState] = useState(currentState)
88

99
function onChange (newState) {
@@ -19,4 +19,4 @@ export default () => {
1919
})
2020

2121
return appState
22-
}
22+
}

lib/useBackHandler.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { useEffect, useState } from 'react'
2+
import { BackHandler } from 'react-native'
3+
4+
5+
export default (handler) => {
6+
useEffect(() => {
7+
BackHandler.addEventListener('hardwareBackPress', handler)
8+
9+
return () => {
10+
BackHandler.removeEventListener('hardwareBackPress', handler)
11+
}
12+
})
13+
}

0 commit comments

Comments
 (0)