将参数传递到滑动/手势上的上一个屏幕

发布于 2025-01-25 10:06:00 字数 643 浏览 2 评论 0原文

我已经彻底阅读了有关使用React导航在屏幕之间传递参数的文档: https://reactnavigation.orgg/docs /params/

但是,所有这些示例仅在您调用导航时起作用。例如:

<Button
  title="Done"
  onPress={() => {
    // Pass and merge params back to home screen
    navigation.navigate({
     name: 'Home',
     params: { post: postText },
     merge: true,
    });
  }}
/>

我有一个带有后背按钮的屏幕,我可以在其中调用navigation.navigate.navigate和通过按钮按下按钮,就像上面的示例一样。但是,用户也可以从左侧滑动以返回Android上的第一个屏幕(我也假设iOS)。

因此,我的问题是:

当用户刷新回去时,我有没有办法将相同的数据传递给上一个屏幕(而不是按下后面的按钮)?

I've thoroughly read the documentation on passing params between screens with React Navigation: https://reactnavigation.org/docs/params/

However, all of those examples only work if you are calling navigation.navigate manually and passing the params. For example:

<Button
  title="Done"
  onPress={() => {
    // Pass and merge params back to home screen
    navigation.navigate({
     name: 'Home',
     params: { post: postText },
     merge: true,
    });
  }}
/>

I have a screen with a back button, where I can call navigation.navigate and pass params on button press, like in the example above. However, the user can also swipe from the left to go back to the first screen on Android (and I'm assuming iOS as well).

So, my question:

Is there a way for me to pass the same data to the previous screen when the user swipes to go back (instead of pressing the back button)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2025-02-01 10:06:01

我可能没有一种更好的方式。但是,我们可以通过

假设您有一个屏幕b,您想向后滑动以转到屏幕home,然后将params传递给home from b在该滑动动作上。然后,您可以如下实现这一目标。

function B({navigation}) {

  React.useEffect(() => navigation.addListener('beforeRemove', (e) => {
         
        // the navigation.navigate will fire beforeRemove which causes an infinite loop. we guard this here
        if (e.data.action.type === "NAVIGATE") {
          return
        }
        // Prevent default behavior of leaving the screen
        e.preventDefault();
        
        // navigate manually
        navigation.navigate({
            name: 'Home',
            params: { post: postText },
            merge: true,
        });        
        
   }), [navigation]);

  return ( ... )
}

编辑: navigation.navigate很明显,beforeRemove事件再次引起无限循环。我们可以如上图所示。

There might be a better way that I am unaware of. However, we can achieve this manually by preventing the default back action and handling this ourselves.

Suppose that you have a screen B and you want to swipe back to go to a screen Home and pass params to Home from B on that swipe action. Then, you can achieve this as follows.

function B({navigation}) {

  React.useEffect(() => navigation.addListener('beforeRemove', (e) => {
         
        // the navigation.navigate will fire beforeRemove which causes an infinite loop. we guard this here
        if (e.data.action.type === "NAVIGATE") {
          return
        }
        // Prevent default behavior of leaving the screen
        e.preventDefault();
        
        // navigate manually
        navigation.navigate({
            name: 'Home',
            params: { post: postText },
            merge: true,
        });        
        
   }), [navigation]);

  return ( ... )
}

Edit: The navigation.navigate fires the beforeRemove event again, obviously, which causes an infinite loop. We can guard this as shown above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文