【问题标题】:Two header button left and right React Navigation v5左右两个标题按钮 React Navigation v5
【发布时间】:2021-05-01 00:12:55
【问题描述】:

我正在努力实现这一目标

Header

headerRight 不起作用,只有 headerLeftheaderTitle 起作用

import Icon from 'react-native-vector-icons/MaterialIcons';

    <HomeStack.Navigator
          screenOptions={{headerStyle: {elevation: 0, shadowOpacity: 0}}}>
          <HomeStack.Screen
            name={NAVIGATOR.ProviderTypeTab}
            component={ProviderTypeStack}
            options={{
              title: null,
              headerLeft: () => (
                <View style={{marginTop: 10, marginLeft: 10}}>
                  <ProfilePicComponent onPress={handleDrawer} />
                </View>
              ),
              headerRight: () => {
                <View style={{height: 10, width: 10, backgroundColor: 'red'}}>
                  {/* <Icon name="location-on" size={24} /> */} //headerRight doesn't work
                </View>;
              },

              headerTitle: () => (
                <View style={{width: '85%', marginTop: 10}}>
                  <SearchInputComponent onPress={handleDrawer} />
                </View>
              ),
            }}
          />
        </HomeStack.Navigator>

【问题讨论】:

    标签: android react-native react-navigation-v5 react-native-vector-icons


    【解决方案1】:

    headerRight 与 headerLeft 相同,但您没有从为 headerRight 提供的函数中返回任何内容

    改变

    headerRight: () => {
                    <View style={{height: 10, width: 10, backgroundColor: 'red'}}>
                      {/* <Icon name="location-on" size={24} /> */} //headerRight doesn't work
                    </View>;
                  },
    

    headerRight: () => (
                    <View style={{height: 10, width: 10, backgroundColor: 'red'}}>
                      {/* <Icon name="location-on" size={24} /> */} //headerRight doesn't work
                    </View>;
                  ),
    

    或者

    headerRight: () => {
                   return  (<View style={{height: 10, width: 10, backgroundColor: 'red'}}>
                      {/* <Icon name="location-on" size={24} /> */} //headerRight doesn't work
                    </View>);
                  },
    

    【讨论】:

    • 它有效!太感谢了。我只是对headerLeft 感到困惑,我没有返回任何东西,但它仍然呈现。
    • 它正在返回,基本上它是一个箭头函数,如果你使用花括号,你必须从函数中返回一些东西,如果你放括号,括号内的值就是返回值
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多