From 539c123858422a59cc73e63530e38e35e37afabb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9D=B0?= Date: Thu, 16 Jan 2025 18:45:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DToast=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=AF=BC=E8=87=B4=E7=89=A9=E7=90=86=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=94=AE=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/cyan-owls-glow.md | 5 +++++ packages/react-native/src/toast/useToast.ts | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 .changeset/cyan-owls-glow.md diff --git a/.changeset/cyan-owls-glow.md b/.changeset/cyan-owls-glow.md new file mode 100644 index 000000000..27b091d0c --- /dev/null +++ b/.changeset/cyan-owls-glow.md @@ -0,0 +1,5 @@ +--- +'@td-design/react-native': patch +--- + +修复Toast组件导致物理返回键无法正常使用的bug diff --git a/packages/react-native/src/toast/useToast.ts b/packages/react-native/src/toast/useToast.ts index 74a219c34..fb5c6e35a 100644 --- a/packages/react-native/src/toast/useToast.ts +++ b/packages/react-native/src/toast/useToast.ts @@ -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'; @@ -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 {