Skip to content

Commit d5d132f

Browse files
committed
feat: created hook useRefreshKeyAfterInteraction
1 parent 2c33975 commit d5d132f

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

src/components/TestDrive/TestDriveBanner.tsx

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import React, {useEffect, useState} from 'react';
2-
import {InteractionManager, View} from 'react-native';
1+
import React from 'react';
2+
import {View} from 'react-native';
33
import Button from '@components/Button';
44
import HeaderGap from '@components/HeaderGap';
55
import Text from '@components/Text';
66
import useLocalize from '@hooks/useLocalize';
7+
import useRefreshKeyAfterInteraction from '@hooks/useRefreshKeyAfterInteraction';
78
import useResponsiveLayout from '@hooks/useResponsiveLayout';
89
import useThemeStyles from '@hooks/useThemeStyles';
9-
import getPlatform from '@libs/getPlatform';
10-
import CONST from '@src/CONST';
1110

1211
type TestDriveBannerProps = {
1312
/** Callback to finish the test drive */
@@ -18,31 +17,20 @@ function TestDriveBanner({onPress}: TestDriveBannerProps) {
1817
const styles = useThemeStyles();
1918
const {shouldUseNarrowLayout} = useResponsiveLayout();
2019
const {translate} = useLocalize();
21-
const [refreshKey, setRefreshKey] = useState(0);
2220

2321
// Forcing a key change here forces React to fully re-mount this component
2422
// This helps reset the underlying drag region boundaries so that
2523
// interactive elements (like buttons) become clickable again after certain UI changes.
2624
// Without this, the OS-level drag region overlap clickable elements,
2725
// making them unresponsive until the container is refreshed.
2826
// For more context: https://github.com/Expensify/App/issues/68139
29-
useEffect(() => {
30-
const isDesktop = getPlatform() === CONST.PLATFORM.DESKTOP;
31-
32-
if (!isDesktop) {
33-
return;
34-
}
35-
36-
InteractionManager.runAfterInteractions(() => {
37-
setRefreshKey((prev) => prev + 1);
38-
});
39-
}, []);
27+
const key = useRefreshKeyAfterInteraction('key');
4028

4129
return (
4230
<View
4331
style={[styles.highlightBG]}
4432
dataSet={{dragArea: false}}
45-
key={refreshKey}
33+
key={key}
4634
>
4735
<HeaderGap styles={styles.testDriveBannerGap} />
4836
<View style={[styles.gap2, styles.alignItemsCenter, styles.flexRow, styles.justifyContentCenter, styles.h10]}>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {useEffect, useState} from 'react';
2+
import {InteractionManager} from 'react-native';
3+
4+
function useRefreshKeyAfterInteraction(defaultValue: string) {
5+
const [counter, setCounter] = useState(0);
6+
7+
useEffect(() => {
8+
InteractionManager.runAfterInteractions(() => {
9+
setCounter((prev) => prev + 1);
10+
});
11+
}, []);
12+
13+
return `${defaultValue}-${counter}`;
14+
}
15+
16+
export default useRefreshKeyAfterInteraction;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function useRefreshKeyAfterInteraction(defaultValue: string) {
2+
return defaultValue;
3+
}
4+
5+
export default useRefreshKeyAfterInteraction;

0 commit comments

Comments
 (0)