简体   繁体   English

使用react hooks,你将如何编写一个依赖于state的事件监听器,而不需要在每次state变化时添加和删除?

[英]Using react hooks, how would you write an event listener dependent on state that does not need to be added and removed every time the state changes?

The code below shows a working, but inefficient implementation of an Android BackHandler within React Native to have the app exit after two presses in two seconds.下面的代码显示了在 React Native 中 Android BackHandler的有效但低效的实现,使应用程序在两秒内按下两次后退出。 This is implemented using React hooks within the main functional component of the app.这是在应用程序的主要功能组件中使用 React hooks 实现的。

However, due to dependency on a state variable recentlyPressedHardwareBack , the useEffect hook will cleanup then run each time the state changes, causing the BackHandler event listener to be detached and re-attached whenever the back button is pressed.但是,由于依赖于 state 变量recentlyPressedHardwareBackuseEffect挂钩将在每次 state 更改时进行清理然后运行,从而导致BackHandler事件侦听器在按下后退按钮时分离并重新附加。 How do you set up this event listener just once without constant creation and deletion, while allowing it to access a changing component state?如何在不不断创建和删除的情况下仅设置一次此事件侦听器,同时允许它访问不断变化的组件 state?

const [recentlyPressedHardwareBack, setRecentlyPressedHardwareBack] =
    useState(false);

  useEffect(() => {
    const backHandler = BackHandler.addEventListener(
      'hardwareBackPress',
      () => {
        // Exit app if user pressed the button within last 2 seconds.
        if (recentlyPressedHardwareBack) {
          return false;
        }

        ToastAndroid.show(
          'Press back again to exit the app',
          ToastAndroid.SHORT,
        );

        setRecentlyPressedHardwareBack(true);

        // Toast shows for approx 2 seconds, so this is the valid period for exiting the app.
        setTimeout(() => {
          setRecentlyPressedHardwareBack(false);
        }, 2000);

        // Don't exit yet.
        return true;
      },
    );

    return () => backHandler.remove();
  }, [recentlyPressedHardwareBack]);

You could use useRef for this.您可以为此使用 useRef。

const recentlyPressedHardwareBackRef = useRef(false);
useEffect(() => {
    const backHandler = BackHandler.addEventListener(
      'hardwareBackPress',
      () => {
        // Exit app if user pressed the button within last 2 seconds.
        if (recentlyPressedHardwareBackRef.current) {
          return false;
        }

        ToastAndroid.show(
          'Press back again to exit the app',
          ToastAndroid.SHORT,
        );

        recentlyPressedHardwareBackRef.current = true;

        // Toast shows for approx 2 seconds, so this is the valid period for exiting the app.
        setTimeout(() => {
          recentlyPressedHardwareBackRef.current = false;
        }, 2000);

        // Don't exit yet.
        return true;
      },
    );

    return () => backHandler.remove();
}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何听 redux state 反应钩子的变化? - How to listen redux state changes in react hooks? React.js钩子从事件监听器设置状态函数 - React.js Hooks Set State Function From Event Listener 为什么更新的 state 没有反映在事件侦听器中:React Native,Hooks - why updated state not reflected inside an event listener: React Native, Hooks 为什么在 React Hooks 中,循环内的 javascript 触发的单击事件不会每次都更新状态? - Why is javascript triggered click event inside a loop not updating state every time in React Hooks? 我需要一个组件来检查状态并在另一个组件更改后重新渲染(使用 React Hooks) - I need a component to check state and rerender after another component changes (using React Hooks) 如何将事件侦听器添加到React Native中的状态 - how to add an event listener to a state in react native React Native / Redux:每次状态更改时如何将更新后的状态传递给子组件? - React Native/Redux: How to pass down updated state to child components every time state changes? React hooks:在侦听器中获取useState状态 - React hooks: get state of useState inside a listener 使用[event.target.name],React组件的状态如何变化? - How does the React component's state changes by using [event.target.name]? 使用反应挂钩更改导入的变量时更新 state - Update state when imported variable changes using react hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM