[英]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.