简体   繁体   English

我如何更改 React Native 中的 Stack 标题?

[英]how I can change the Stack title in react native?

I have a header.js file in my shared folder.我的共享文件夹中有一个 header.js 文件。 I want it to use it on every stack to design the Header and Information.我希望它在每个堆栈上使用它来设计 Header 和信息。 In my App.js I load the header like this:在我的 App.js 中,我像这样加载 header:

App.js
...
    <Stack.Navigator screenOptions={{
      headerTitle: () => <Header /> (<-- Here the Header.js file )
    }} initialRouteName="AppTabs">
      <Stack.Screen name="AppTabs" component={AppTabs} />
    </Stack.Navigator>

Header.js Header.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Entypo } from '@expo/vector-icons'; 

const Header = ({ navigation }) => {
  console.log(navigation);
  return (
    <View style={styles.header}>
      <Entypo name="menu" size={24} color="black" />
      <View>
        <Text style={styles.headerText}>TITEL TO CHANGE BUT HOW?</Text>
      </View>
    </View>
  )
};

How Can I change the title automaticly like this:我怎样才能像这样自动更改标题:

<Text>{title}</Text>

Your Header.js should look like this.你的Header.js应该是这样的。

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Entypo } from '@expo/vector-icons';

const Header = ({ children }) => {
  console.log(children);
  return (
    <View style={styles.header}>
      <Entypo name="menu" size={24} color="black" />
      <View>
        <Text style={styles.headerText}>{children}</Text>
      </View>
    </View>
  );
};

Your Stack.Navigator should look like this -你的Stack.Navigator应该是这样的 -

<Stack.Navigator
  screenOptions={{
    headerTitle: (props) => <Header {...props} />, // this prop contains screen name
  }}
  initialRouteName="AppTabs">
  <Stack.Screen name="AppTabs" component={AppTabs} />
</Stack.Navigator>;

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

相关问题 如何在 React Native 中更改导航栏标题 - How can i change Navigationbar title in React Native 如何动态更改React Native堆栈导航屏幕的标题? - How do I dynamically change the title of React Native stack navigation screens? 我如何在本机反应中访问组件的内部属性(标题) - how can i access the inside property (title) of an component in react native React Native - 如何将 function 传递到导航器中的堆栈屏幕? - React Native - how can I pass a function to a stack screen in a navigator? 如何将 props 传输到位于堆栈中的组件 react native? - How can I transfer props to components located in stack react native? 如何在 Vite React 中动态更改标题? - How can I dynamically change title in Vite React? 无法在 React Native Paper 中更改卡片标题文本的颜色 - Can't change color of Card Title text in React Native Paper 基于从嵌套抽屉导航中选择的内容的本机更改堆栈导航器标题 - React-native change stack navigator title based on what is selected from nested drawer navigation 如何在本机反应中更改堆栈导航器中的后退按钮图像? - How do I change the back button image in stack navigator in react native? 如何在React Native中将标签栏更改为分段控件? - How can I change a tab bar to a segmented control in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM