react native navigationOptions中不能獲取this


static navigationOptions = ({ navigation, navigationOptions,screenProps }) => {
const { params } = navigation.state;
return {
tabBarVisible: true,
headerRight:(
<View>
<TouchableOpacity onPress={()=>navigation.state.params.navigatePress()}>
//this.order()
<View style={{marginRight:30/1536*width}}>
<Text style={styles.hadlesub}>點餐</Text>
</View>
</TouchableOpacity>
</View>
)
}
};

// 點擊完成按鈕
order = ()=> {
alert('haha');
}

componentDidMount() {
// 處理數據源
this.props.navigation.setParams({navigatePress:this.order})
}

方法二:

在最外部聲明:
let that;
在class內部:
constructor(props){ super(props); that = this; }
在const * = StackNavigation({})中:
static navigationOptions = ({ navigation, navigationOptions,screenProps }) => {
const { params } = navigation.state;
return {
tabBarVisible: true,
headerRight:(
<View>
<TouchableOpacity onPress={() => that.order()}>
<View style={{marginRight:30/1536*width}}>
<Text style={styles.hadlesub}>點餐</Text>
</View>
</TouchableOpacity>
</View>
)
}
};

order = ()=> {
alert('haha');
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM