【问题标题】:React Navigation headerRight not visibleReact Navigation headerRight 不可见
【发布时间】:2021-02-07 13:46:26
【问题描述】:

我正在尝试在屏幕标题标题右侧呈现一个按钮。但我没有得到想要的结果。我已经推荐了 react-navigation 官方doc。这是我屏幕的screenshot
这是我导航的代码 sn-p。

 <NavigationContainer>
            <Stack.Navigator screenOptions={{
                headerStyle: {
                    backgroundColor: Colors.header.backgroundColor,
                },
                headerTintColor: Colors.header.fontColor,
                headerTitleAlign: 'left',
                headerTitleStyle: {
                    fontSize: Fonts.size.regular
                }
            }}>
                <Stack.Screen name='Home' component={Home} options={{
                    headerTitle: 'Seguro',
                    headerRight: () => {
                        <Button              // I want to render this button
                            onPress={() => console.log('This is a button!')}
                            title="Info"
                            color="#fff"
                        />
                    }
                }} />
                <Stack.Screen name='Login' component={Login} />
            </Stack.Navigator>
        </NavigationContainer>

【问题讨论】:

    标签: react-native react-navigation react-navigation-stack


    【解决方案1】:

    问题是你没有返回任何东西。

    选项1:隐式返回..括在括号中使其隐式返回

    <Stack.Screen name='Home' 
         component={Home} 
         options={{
                        headerTitle: 'Seguro',
                        headerRight: () => (
                            <Button             
                                onPress={() => console.log('This is a button!')}
                                title="Info"
                                color="#fff"
                            />
                        )
                }} />
    

    选项 2:显式返回

    <Stack.Screen name='Home' 
         component={Home} 
         options={{
                        headerTitle: 'Seguro',
                        headerRight: () => { 
                           return (
                            <Button             
                                onPress={() => console.log('This is a button!')}
                                title="Info"
                                color="#fff"
                            />
                        )
                  }
                }} />
    

    【讨论】:

    • 该死!你拯救了我的一天。我怎么能错过这个东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多