File tree Expand file tree Collapse file tree 4 files changed +34
-3
lines changed Expand file tree Collapse file tree 4 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ yarn add react-native-hooks
21
21
## API
22
22
- [ useAccessibilityInfo] ( https://github.com/react-native-community/react-native-hooks#useaccessibilityinfo )
23
23
- [ useAppState] ( https://github.com/react-native-community/react-native-hooks#useappstate )
24
+ - [ useBackHandler] ( https://github.com/react-native-community/react-native-hooks#usebackhandler )
24
25
- [ useCameraRoll] ( https://github.com/react-native-community/react-native-hooks#usecameraroll )
25
26
- [ useClipboard] ( https://github.com/react-native-community/react-native-hooks#useclipboard )
26
27
- [ useDimensions] ( https://github.com/react-native-community/react-native-hooks#usedimensions )
@@ -45,6 +46,21 @@ import { useAppState } from 'react-native-hooks'
45
46
const currentAppState = useAppState ()
46
47
```
47
48
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
+
48
64
### ` useCameraRoll `
49
65
50
66
``` js
Original file line number Diff line number Diff line change 1
1
import useDimensions from './lib/useDimensions'
2
2
import useAppState from './lib/useAppState'
3
+ import useBackHandler from './lib/useBackHandler'
3
4
import useCameraRoll from './lib/useCameraRoll'
4
5
import useClipboard from './lib/useClipboard'
5
6
import useAccessibilityInfo from './lib/useAccessibilityInfo'
@@ -11,6 +12,7 @@ import useLayout from './lib/useLayout'
11
12
export {
12
13
useDimensions ,
13
14
useAppState ,
15
+ useBackHandler ,
14
16
useCameraRoll ,
15
17
useClipboard ,
16
18
useAccessibilityInfo ,
Original file line number Diff line number Diff line change 1
1
import React , { useEffect , useState } from 'react'
2
2
import { AppState } from 'react-native'
3
3
4
- const currentState = AppState . currentState
5
-
4
+ // https://github.com/facebook/react-native/issues/18836
6
5
export default ( ) => {
6
+ const currentState = AppState . currentState
7
7
const [ appState , setAppState ] = useState ( currentState )
8
8
9
9
function onChange ( newState ) {
@@ -19,4 +19,4 @@ export default () => {
19
19
} )
20
20
21
21
return appState
22
- }
22
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments