如何在反应导航中访问this.props?

时间:2017-10-22 16:46:52

标签: react-native react-navigation

我在我的React Native应用程序中使用react navigation,当用户单击App右上角的Save按钮时,我想调用onSavePress()函数。

但是当我尝试调用onSavePress函数时,我的应用程序崩溃,换句话说,它不像我从静态navigationOptions函数中调用该函数。

以下是以下代码:

class TheFormScreen extends Component {

  constructor(props)
  {
    super(props);
    this.state = {
        visible: true
      };
  }

  state = {
      isReady: false,
    };


static navigationOptions = ({ navigation }) => ({
    title: `${navigation.state.params.item.a1}`,
    tabBarIcon:  ({ tintColor }) => {
          return <Icon name="phone" size={30} color={tintColor} />;
    },
    headerRight: (
      <Btn
        title='Done-'
        onPress={() => { console.log(' on Press done on line 73 Form--Screen    this.props=', this.props);  navigation.goBack()   }
         }
      />
    ),
  });

onDecline(){
  // console.log(' onDecline  clicked');
  this.setState ({ showModal : false });
}



}
onDeleteProperty() {
  // console.log(' onDelete  clicked');

}


onSavePress(){
    this.onClickToaster();
    console.log( '101 - on click onSavePress ');
    const { a1, a2 } = this.props;

    this.props.propertySave({  a1, a2 });
}

1 个答案:

答案 0 :(得分:0)

所以,这只是所有代码的明确片段,广告infoSave也没有,而且onSavePress也是如此。

我假设您的问题是this.props未定义或类似的东西。如果是这种情况,您可以将此函数更改为箭头函数:

onSavePress = () => {
    this.onClickToaster('Saving the info....', 'top' , DURATION.LENGTH_LONG, true);
    const { a1, a2 } = this.props;
    this.props.infoSave({  a1, a2 });
}

编辑:

你是说这个?

 headerRight: (
  <Btn
    title='Save'
    onPress={() => { this.onSavePress();  navigation.goBack()   }
     }
  />
),
相关问题