使用react-navigation时,单页面设置navigationOptions中,进行Static中调用方法,不能像以下设置
onPress = {()=>this.navigatePress()}
static navigationOptions = ({navigation, screenProps}) => ({
headerTitle: '登录',
headerLeft:(
<Text onPress={()=>this.navigatePress()} style={{marginLeft:5, width:30, textAlign:"center"}} >
<Icon name='ios-arrow-back'size={24} color='white' />
</Text>
)
});
解决方法:
static navigationOptions = ({navigation, screenProps}) => ({
headerTitle: '登录',
headerLeft:(
<Text onPress={()=>navigation.state.params.navigatePress()} style={{marginLeft:5, width:30, textAlign:"center"}} >
<Icon name='ios-arrow-back'size={24} color='white' />
</Text>
)
});
_onBackAndroid=()=>{
alert('点击headerLeft');
}
componentDidMount(){
//在static中使用this方法
this.props.navigation.setParams({ navigatePress:this._onBackAndroid })
}