在某个路线上,通过反应进行渲染一个组件

发布于 2025-02-08 02:28:19 字数 1474 浏览 2 评论 0原文

我有一个带有React路由器的堆栈导航器,使用@react-navigation/native-stack。我的主屏幕是一张地图,我有一些菜单组件:

export const App = () => {
  return (
      <NavigationContainer>
        <Stack.Navigator initialRouteName="Home">
          <Stack.Screen
            name="Home"
            options={{ headerShown: false }}
            component={Home}
          />
          <Stack.Screen
            name="SignIn"
            component={SignIn}
          />
        </Stack.Navigator>
      </NavigationContainer>
  )
}

home是地图组件,signin以及其他一些组件是菜单组件导航到屏幕上,按预期将HOME推出。

我希望能够在某个路由上“导航”到另一个组件,将其称为Overlay。当在此处路由时,我想用覆盖组件放置在home> home组件的一部分上,同时仍在与该部分保持交互性,主屏幕未覆盖。但是,如果我这样做:

       <Stack.Navigator initialRouteName="Home">
          <Stack.Screen
            name="Home"
            options={{ headerShown: false }}
            component={Home}
          />
          <Stack.Screen
            name="Overlay"
            component={Overlay}
          />
        </Stack.Navigator>

那么home屏幕移开,并由Overlay代替。

当用户被路由到某个路由时,如何显示覆盖在我的主页屏幕顶部的组件?这是对React-Router很容易完成的事情,但是我不确定React-Navigation是否存在相似之处。反应导航不是正确的方法吗?有条件地渲染基于状态/redux变量的覆盖组件更好吗?

I have a stack navigator with react router, using @react-navigation/native-stack. My home screen is a map, and I have a few menu components:

export const App = () => {
  return (
      <NavigationContainer>
        <Stack.Navigator initialRouteName="Home">
          <Stack.Screen
            name="Home"
            options={{ headerShown: false }}
            component={Home}
          />
          <Stack.Screen
            name="SignIn"
            component={SignIn}
          />
        </Stack.Navigator>
      </NavigationContainer>
  )
}

Home is the map component, and SignIn, as well as some other components, are menu components that take over the screen when navigated to, pushing Home out of the way, as expected.

I want to be able to 'navigate' to another component at a certain route, call it Overlay. When routed here, I want to show the home screen, with an Overlay component laid over the top of part of the Home component, while still maintaining interactivity with the part of the Home screen that is not covered. However, if I do this:

       <Stack.Navigator initialRouteName="Home">
          <Stack.Screen
            name="Home"
            options={{ headerShown: false }}
            component={Home}
          />
          <Stack.Screen
            name="Overlay"
            component={Overlay}
          />
        </Stack.Navigator>

Then the Home screen moves away, and is replaced by Overlay.

How can I show a component overlaid over the top of my Home screen when the user is routed to a certain route? This is something easily done with react-router, but I'm not sure if there is a parallel in react-navigation. Is react-navigation not the right approach for this? Is it better to conditionally render the Overlay component based on a state/redux variable?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文