从React Navigation v4升级到v5时获取默认样式

问题描述 投票:0回答:1

我目前正在使用React Navigation v4并迁移到v5。我正在使用official documentation进行升级,但是很遗憾,我遇到了阻止程序。

在V4中,我可以执行以下操作:

export default function ExampleScreen(props) {
  return <View></View>
}

ExampleScreen.navigationOptions = ({navigation, navigationOptions}) => ({
  headerStyle: {
    ...navigationOptions.headerStyle,
    borderBottomWidth: 0
  },
  headerRight: () => <SearchBox navigation={navigation} />
})

但是在V5中,我似乎无法访问navigationOptions参数,因此我无法获得navigationOptions.headerStyle

export default function ExampleScreen(props) {
  props.navigation.setOptions({
    headerStyle: {
      // I can't get the default styles here.
      borderBottomWidth: 0
    },
    headerRight: () => <SearchBox navigation={props.navigation} />
  })

  return <View></View>
}

我如何在React Navigation V5中做到这一点,因为在其他任何地方都没有记录?

reactjs react-native react-navigation react-navigation-v5
1个回答
0
投票

将默认值放入变量并导出。然后导入您需要的位置并使用:

export const headerStyle = {
  /* whatever */
};

// Use in `screenOptions`
<Stack.Navigator screenOptions={{ headerStyle }}></Stack.Navigator>;

// Use in `setOptions`
navigation.setOptions({
  headerStyle: [headerStyle, { borderBottomWidth: 0 }],
  headerRight: () => <SearchBox navigation={props.navigation} />
});
© www.soinside.com 2019 - 2024. All rights reserved.