React native 即使使用“清理”功能,也会对本机深度链接不需要的组件渲染作出反应

React native 即使使用“清理”功能,也会对本机深度链接不需要的组件渲染作出反应,react-native,react-hooks,deep-linking,React Native,React Hooks,Deep Linking,我的深度链接句柄URL有问题,它是我的组件在URL到达我的应用程序后多次呈现的。换句话说,假设在客户端选择所需的会话时间(由状态处理)后,浏览器打开进行购买操作,并且在客户端决定进行购买或取消购买后,应用程序打开以显示结果,但它会多次执行该操作 以下是组件的一部分: const bookingHandler = () => { if(selected == null) { Alert.alert('Invalid Selection', 'Please select o

我的深度链接句柄URL有问题,它是我的组件在URL到达我的应用程序后多次呈现的。换句话说,假设在客户端选择所需的会话时间(由状态处理)后,浏览器打开进行购买操作,并且在客户端决定进行购买或取消购买后,应用程序打开以显示结果,但它会多次执行该操作

以下是组件的一部分:

const bookingHandler = () => {
    if(selected == null) {
      Alert.alert('Invalid Selection', 'Please select one of the sessions!')
      return;
    }
    Linking.openURL('http://www.medicalbookingapp.cloudsite.ir/sendPay.php');
  }

  selectedTimeIndex = therapists.meetingPlans[selectedPlanIndex].times.findIndex((el) => el.time == selected.title);
 
  useEffect(() => {

    Linking.addEventListener('url', (e) => handleOpenUrl(e.url))

    return () => {
      Linking.removeEventListener('url', (e) => handleOpenUrl(e.url));
      console.log('event removed')
    } 
    
  });

  const handleOpenUrl = (url) => { 
    const route = url.replace(/.*?:\/\/\w*:\w*\/\W/g, '') // exp://myapplication.... --> ''
    const id = route.split('=')[1]
    
    if(id == 1) {
      dispatch(
        BookingActions.addBooking(
          therapistId,
          therapistFirstName,
          therapistLastName,
          selected.title,
          moment(selectedDate).format("YYYY-MMM-DD"),
          selected.slots,
        )
      );
      

      dispatch(
        doctorActions.updateTherapists(therapistId, selected.slots, selectedDate, selected.title, selectedPlanIndex, selectedTimeIndex)
      );
      setBookingConfirm(true)
      toggleModal();

    } else if(id == 0) {
      console.log('purchase failed...');
      toggleModal();
    }
  }
我在
useffect
钩子中处理我的侦听器,它还具有清理功能。但在控制台中,我得到了以下错误:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s, a useEffect cleanup function.
此外,我还注意到了一些其他问题,为了抓住问题,您可以看到组件屏幕,会话选择部分是用
状态处理的。渲染
useffect
hook的数量与选择会话状态的更改数量有一定的关系。(我的意思是,如果我在选择会话周围反弹,它将渲染更多)

如果你需要更多的信息,请在评论中告诉我。
任何帮助都将不胜感激。

一个可能的问题是您可能要创建两个处理程序,一个在addListener中,另一个在RemovelListener中

你能试试吗

consthandler=(e)=>handleopeanurl(e.url);
Linking.addEventListener('url',handler);
return()=>{
Linking.removeEventListener('url',handler);
console.log('事件已删除')
} 

Javascript将两个事件侦听器回调视为单独的实体(原因:对象引用)。若要解决此问题,请在外部声明回调函数,并将其传递给addEventListener和removeEventListener。@HarinderSingh谢谢兄弟。我真的很感激。谢谢,我真的很感激,你能告诉我(比如im 5)在eventListener中使用处理程序有什么问题吗?