Skip to content

Commit

Permalink
fix: 修复Toast组件导致物理返回键无法正常使用的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chj-damon committed Jan 16, 2025
1 parent 2bce597 commit 539c123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-owls-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': patch
---

修复Toast组件导致物理返回键无法正常使用的bug
13 changes: 8 additions & 5 deletions packages/react-native/src/toast/useToast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState } from 'react';
import { BackHandler } from 'react-native';
import { BackHandler, NativeEventSubscription } from 'react-native';

import { useMemoizedFn } from '@td-design/rn-hooks';

Expand Down Expand Up @@ -45,11 +45,14 @@ export default function useToast() {

/** 当Toast显示的时候,不允许安卓物理返回键可用 */
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', () => visible);
let backHandler: NativeEventSubscription | undefined;
if (visible) {
backHandler = BackHandler.addEventListener('hardwareBackPress', () => true);
} else {
backHandler?.remove();
}

return () => {
BackHandler.removeEventListener('hardwareBackPress', () => false);
};
return () => backHandler?.remove();
}, [visible]);

return {
Expand Down

0 comments on commit 539c123

Please sign in to comment.