首页 > 解决方案 > componentdidmount 中的 this.props.navigation.setparams 未定义

问题描述

您好,我正在尝试更改 componentDidMount 中 navigationOptions 的 headerRight 但是道具未定义。我相信我做对了它被称为 setParams 的方式。这是我的代码:

componentDidMount(){
try{
  AsyncStorage.getItem('experience').then(experience => {
    const objExperience = new Experience();
    const newExperience = JSON.parse(experience);

    showExperience = objExperience.showMyLevel((newExperience));

    this.props.navigation.setParams({
      headerTitle: 'Social',
      headerLeft:  <Icon style={{paddingLeft: 20}}
                         name="md-menu" 
                         size={30} 
                         onPress={()=> this.props.navigation.openDrawer()}/>,
      headerRight: <Text style={{paddingRight: 20}}>Level {showExperience}</Text>,
    });
  });
}
catch(e){
  console.log('error from AsyncStorage in' + arguments.callee.name + ': ', e);
}
}
const Social = createStackNavigator({
 SocialScreen:{
   screen: SocialScreen,
   navigationOptions: ({navigation})=>{
     const { params } = navigation.state;

     return params;
   }
 },
 Detail:{
   screen: Detail
 }
});

社交在主要组件内部被调用,所以我相信范围可能与我需要的不同。

const DashboardTabNavigator = createBottomTabNavigator({
  Social,
  Work,
  ToDo
},
{
navigationOptions: ({navigation})=>{
  const {routeName} = navigation.state.routes[navigation.state.index];

  return {
    header: null,
    headerTitle: routeName, //it puts the name of the tab on the top of the 
    screen.
  };
}
});

标签: react-nativereact-native-navigation

解决方案


推荐阅读