【问题标题】:Getting default styles when upgrading from React Navigation v4 to v5从 React Navigation v4 升级到 v5 时获取默认样式
【发布时间】:2020-05-25 10:02:45
【问题描述】:

我目前正在使用 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】:

    将默认值放入变量并导出。然后在需要的地方导入并使用:

    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} />
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 2022-11-17
      • 2022-09-23
      • 1970-01-01
      • 2021-05-22
      • 2021-11-07
      • 2017-09-21
      • 2022-08-16
      相关资源
      最近更新 更多