Skip to content

Commit 9810c16

Browse files
authored
Update useBackHandler.js
Every time this function was called you will add a new `addEventListener`, now we passed an argument to create a new listener only when receive a new function
1 parent 7abd32f commit 9810c16

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/useBackHandler.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import React, { useEffect } from 'react'
1+
import { useEffect} from 'react'
22
import { BackHandler } from 'react-native'
33

4-
export default function useBackHandler(handler) {
4+
export default function useBackHandler(handler: () => boolean) {
55
useEffect(() => {
66
BackHandler.addEventListener('hardwareBackPress', handler)
7-
87
return () => {
9-
BackHandler.removeEventListener('hardwareBackPress', handler)
10-
}
11-
})
8+
BackHandler.removeEventListener(
9+
'hardwareBackPress',
10+
handler
11+
);
12+
};
13+
}, [ handler ])
1214
}

0 commit comments

Comments
 (0)