如何使用堆栈屏幕标头和headright导航本机页面?

发布于 2025-01-24 05:30:11 字数 2773 浏览 2 评论 0原文

我正在研究一个React Native项目,我想自定义React stack Navigator。堆栈导航器屏幕的默认“返回按钮”给出了上一个屏幕和后箭头的名称。我想更改名称,更重要的是删除后背胡萝卜。

我访问了React Native文档,答案似乎正在使用“ HeaderLeft” 选项,但我不知道如何导航回到上一个屏幕。如果我使用onpress = {()=> navigation.navigate({routename:'home'})}

然后我遇到一个错误,说reference> reference eRror:找不到变量:navigation

,我如何使用headerLefter导航我的堆栈导航器和Heartright?

给出错误的代码是:

      <Stack.Screen
        name="ChoresOptionsPage"
        component={ChoresOptionsPage}
        options={{
          headerBackTitle: "Cancel",
          headerTitle: "Add Chores",
          headerLeft: () => (
            <Button
              onPress={() => navigation.navigate({routeName: 'Home'})}
              title="Cancel"
              color="#fff"
            />
          ),
        }}
      />

这是我的文件代码: - app.js-

const Stack = createStackNavigator();


export default function App() {
  return (
    <NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerStyle: {
          backgroundColor: "#FFA06A"
        },
        headerTintColor: "#fff",
        headerTitleStyle: {
          fontWeight: "bold",
        },
      }}
    >
      <Stack.Screen
        name="Home"
        component={Home}
        options={{
          headerTitle: (props) => <Title></Title>,
        }}
      />
      <Stack.Screen
        name="ChoresOptionsPage"
        component={ChoresOptionsPage}
        options={{
          headerBackTitle: "Cancel",
          headerTitle: "Add Chores",
          headerLeft: () => (
            <Button
              onPress={() => navigation.navigate({routeName: 'Home'})}
              title="Cancel"
              color="#fff"
            />
          ),
        }}
        
      />
      <Stack.Screen
        name="ChoreLibrary"
        component={ChoreLibrary}
        options={{
          headerTitle: "Chore Library",
          headerBackTitle: "Cancel",
          headerRight: () => (
            <Button
              onPress={() => alert('This is a button!')}
              title="Save"
              color="#fff"
            />
          ),
        }}
      />
            <Stack.Screen
        name="CustomChore"
        component={CustomChore}
        options={{
          headerTitle: "Custom Chore",
          headerBackTitle: "Cancel",
          headerRight: () => (
            <Button
              onPress={() => alert('This is a button!')}
              title="Save"
              color="#fff"
            />
          ),
        }}
      />

    </Stack.Navigator>
  </NavigationContainer>
  )
    
}

I am working on a react native project and I would like to customize the react stack navigator. The default "back button" of a stack navigator screen gives the name of the previous screen and a back arrow. I would like to change the name and more importantly remove the back button carrot.

I visited the React Native documentation and the answer seems to be using the "headerLeft" option, but I don't know how to navigate back to my previous screen. If I use onPress={() => navigation.navigate({routeName: 'Home'})}

then I get an error saying ReferenceError: Can't find variable: navigation

So how I can I navigate my stack navigator using headerLeft and headerRight?

The piece of code giving the error is:

      <Stack.Screen
        name="ChoresOptionsPage"
        component={ChoresOptionsPage}
        options={{
          headerBackTitle: "Cancel",
          headerTitle: "Add Chores",
          headerLeft: () => (
            <Button
              onPress={() => navigation.navigate({routeName: 'Home'})}
              title="Cancel"
              color="#fff"
            />
          ),
        }}
      />

Here is my file code: -- App.js --

const Stack = createStackNavigator();


export default function App() {
  return (
    <NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerStyle: {
          backgroundColor: "#FFA06A"
        },
        headerTintColor: "#fff",
        headerTitleStyle: {
          fontWeight: "bold",
        },
      }}
    >
      <Stack.Screen
        name="Home"
        component={Home}
        options={{
          headerTitle: (props) => <Title></Title>,
        }}
      />
      <Stack.Screen
        name="ChoresOptionsPage"
        component={ChoresOptionsPage}
        options={{
          headerBackTitle: "Cancel",
          headerTitle: "Add Chores",
          headerLeft: () => (
            <Button
              onPress={() => navigation.navigate({routeName: 'Home'})}
              title="Cancel"
              color="#fff"
            />
          ),
        }}
        
      />
      <Stack.Screen
        name="ChoreLibrary"
        component={ChoreLibrary}
        options={{
          headerTitle: "Chore Library",
          headerBackTitle: "Cancel",
          headerRight: () => (
            <Button
              onPress={() => alert('This is a button!')}
              title="Save"
              color="#fff"
            />
          ),
        }}
      />
            <Stack.Screen
        name="CustomChore"
        component={CustomChore}
        options={{
          headerTitle: "Custom Chore",
          headerBackTitle: "Cancel",
          headerRight: () => (
            <Button
              onPress={() => alert('This is a button!')}
              title="Save"
              color="#fff"
            />
          ),
        }}
      />

    </Stack.Navigator>
  </NavigationContainer>
  )
    
}

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

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

发布评论

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

评论(1

黯然 2025-01-31 05:30:11

我认为您可以将期望导航对象的函数传递到 options prop,

因此,对于您的代码,结果将是:

<Stack.Screen
  name="ChoresOptionsPage"
  component={ChoresOptionsPage}
  options={({navigation}) => ({
    headerBackTitle: "Cancel",
    headerTitle: "Add Chores",
    headerLeft: () => (
      <Button
        onPress={() => navigation.navigate({routeName: 'Home'})}
        title="Cancel"
        color="#fff"
      />
    ),
    })}
/>

I think you can pass a function that expects the navigation object, to the options prop, kind of what is done in this example from the docs.

So the result, for your piece of code, would be something like:

<Stack.Screen
  name="ChoresOptionsPage"
  component={ChoresOptionsPage}
  options={({navigation}) => ({
    headerBackTitle: "Cancel",
    headerTitle: "Add Chores",
    headerLeft: () => (
      <Button
        onPress={() => navigation.navigate({routeName: 'Home'})}
        title="Cancel"
        color="#fff"
      />
    ),
    })}
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文