[英]How do I dynamically change the title of React Native stack navigation screens?
I'm using stack navigation for a React Native app, and I want to dynamically change the title of a screen when you go to it from another screen. 我正在为React Native应用程序使用堆栈导航,并且当您从另一个屏幕转到该屏幕时,我想动态更改该屏幕的标题。
My navigation is set up as follows in App.js
: 我的导航在
App.js
设置如下:
const navigator = createStackNavigator({
Home: HomeScreen,
ResultDetails: ResultDetailsScreen
}, {
initialRouteName: 'Home',
defaultNavigationOptions: {
title: 'App Name'
}
});
On the HomeScreen
, I have a FlatList
of TouchableOpacity
elements that link to the ResultDetailsScreen
as follows: 在
HomeScreen
,我有一个FlatList
的TouchableOpacity
链接到的元素ResultDetailsScreen
如下:
<FlatList
data={results}
keyExtractor={ (result) => result.id }
renderItem={ ({ item }) => {
return (
<TouchableOpacity
onPress={ () => navigation.navigate('ResultDetails', item) }
>
<Text>{ item.name }</Text>
</TouchableOpacity>
);
} }
/>
The item
I'm sending to the ResultDetailsScreen
has both id
and name
properties. 该
item
我发送到ResultDetailsScreen
既有id
和name
属性。
How do I take the name
property of item
and dynamically set that for the stack navigation title on the ResultDetailsScreen
? 如何获取
item
的name
属性,并在ResultDetailsScreen
上为堆栈导航标题动态设置该name
属性? I've looked at related topics on SO, etc., but I can't seem to get it to work. 我已经看过SO等等的相关主题,但是我似乎无法使其正常工作。
I've seen some stuff about maybe using the following: 我看过一些有关使用以下内容的信息:
navigation.setParams({
title: 'New Title'
});
But when I do that, I don't see New Title
on the ResultDetailsScreen
, and for whatever reason, I can no longer go back in the app. 但是当我这样做时,我在
ResultDetailsScreen
不到“ New Title
”,并且由于某种原因,我无法再返回该应用程序。
Lastly, if it makes a difference, I'm using functions and hooks, not classes for my React Native code. 最后,如果有所作为,我将在我的React Native代码中使用函数和钩子,而不是类。
you have to create a static function in which screen you have to set dynamic name 您必须创建一个静态函数,在其中必须设置动态名称的屏幕
static navigationOptions = ({ navigation }) => { navOptions = navigation return { title: 'Notification', } }
When using functions for React Native components instead of classes, you have to do the following: 在将函数用于React Native组件而不是类时,您必须执行以下操作:
First, send the data you want to use for the page title to the page using navigation.navigate
. 首先,使用
navigation.navigate
将要用于页面标题的数据发送到页面。 For example: 例如:
<FlatList
data={results}
keyExtractor={ (result) => result.id }
renderItem={ ({ item }) => {
return (
<TouchableOpacity
onPress={ () => navigation.navigate('ResultDetails', {
name: item.name
}) }
>
<Text>{ item.name }</Text>
</TouchableOpacity>
);
} }
/>
Then, on the page itself, you can add navigationOptions
as a property of the screen function as follows: 然后,可以在页面本身上,将
navigationOptions
添加为屏幕功能的属性,如下所示:
const ResultDetailsScreen = ({ navigation }) => {
// Return your view here like you always do.
};
// Add this.
// Do this to get the name property to be dynamically set for the page title.
ResultDetailsScreen.navigationOptions = ({ navigation }) => {
const name = navigation.getParam('name');
return {
title: name
};
};
export default ResultDetailsScreen;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.