【问题标题】:react navigation v3 set screenProps through AppContainerreact navigation v3 通过 AppContainer 设置 screenProps
【发布时间】:2018-11-19 15:11:08
【问题描述】:

我需要将主题设置传递给每个屏幕的导航选项。设置存储在 redux 中。在旧版本中,我会写这样的东西:

const AppNavigator = StackNavigator({
  Home: { screen: MainList },
  Settings: { screen: Settings },
  NewEvent: { screen: NewEvent },
  Contacts: { screen: Contacts },
  Map: { screen: Map},
})

在渲染函数中...

<AppNavigator 
   screenProps={{
     settings: this.props.settings,
     headerTintColor: '#ffffff'
   }}
/>

...props.settings 是 redux 状态的一部分。 Wich 在 navigationOptions 中使用

static navigationOptions = ({ navigation, screenProps }) => {
  let settings = screenProps.settings;
  let theme = settings? settings.theme: null;

  return {
    title: strings.account,
    headerTintColor: screenProps.headerTintColor,
    headerStyle: {backgroundColor: theme? theme.def.primaryColor: null}
  };
};

但是当我必须用 createAppContainer 包装我的导航器时,我现在应该怎么做呢?使用 navigation.setParams 会在模拟器中造成明显的延迟,所以它对我来说不是一个合适的解决方案......请记住,主题可以随时更改!

【问题讨论】:

    标签: reactjs react-native redux react-navigation


    【解决方案1】:

    嗯)结果你可以通过创建这样的自定义导航器来实现:

    class NavWrapper extends Component {
      static router = AppNavigator.router;
      render() {
        const { navigation } = this.props;
    
        return <AppNavigator
          navigation={navigation}
          screenProps={{
            settings: this.props.settings,
            headerTintColor: '#ffffff'
          }}
        />;
      }
    }
    
    const AppNavWrapper = connect(state => ({settings: state.settings}))(NavWrapper);
    const AppContainer = createAppContainer(AppNavWrapper);
    

    然后在你的根组件中

    render() {
      return (
        <AppContainer/>
      );
    }
    

    希望有帮助:)

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2020-06-05
      • 2019-11-07
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多