简体   繁体   English

如何动态更改React Native堆栈导航屏幕的标题?

[英]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 ,我有一个FlatListTouchableOpacity链接到的元素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既有idname属性。

How do I take the name property of item and dynamically set that for the stack navigation title on the ResultDetailsScreen ? 如何获取itemname属性,并在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 can set the title when you navigate to the screen 您可以在导航到屏幕时设置标题

navigation.navigate('ResultDetails', { title: 'New Title' });

More here 这里更多

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.

相关问题 在 React Native Navigation 中,如何将道具发送到我的屏幕? - In React Native Navigation, how do I send props to my screens? 我如何更改 React Native 中的 Stack 标题? - how I can change the Stack title in react native? 如何将 React Native Navigation Stack 与 class 组件一起使用? - How do I use the React Native Navigation Stack with class components? 如何正确定义 React Native Navigation 的屏幕? (错误:找不到导航器的任何屏幕。您是否定义了任何屏幕...) - How do I properly define screens for React Native Navigation? (Error: Couldn't find any screens for the navigator. Have you defined any screens...) 如何将公共屏幕添加到 react-navigation - How do I add public screens to react-navigation 基于从嵌套抽屉导航中选择的内容的本机更改堆栈导航器标题 - React-native change stack navigator title based on what is selected from nested drawer navigation 如何在 React Native 屏幕之间共享状态? - How do I share state between react native screens? 我如何使屏幕适合本机中的设备 - How do I make the screens to fit in a device in react native 在 2 个屏幕之间反应本机导航 - React native navigation between 2 screens React Native 动态添加屏幕 - React Native add Screens dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM