我有一个 react-native 应用程序,我需要根据用户位置呈现一个菜单项,但是返回用户位置的函数是一个异步函数,我得到一个未定义的,所以我的问题是如何我根据用户位置设置 navigationOptions
的 drawerLabel
值?
static navigationOptions = () => ({
drawerLabel: () => (OptionsDrawer.isOptionsItemAvaliable() ? 'Options' : null)
})
private static isOptionsItemAvaliable() {
navigator.geolocation.getCurrentPosition((result) => this.setUserLocation(result.coords), ()=> {console.log("Failed to load user location")};
return verifyUserLocation(userLocation);
}
private static setUserLocation(coordinates){
userLocation = coordinates; //userLocation is a global variable
}
private static verifyUserLocation(coordinates){
//Code to check the user location and returns if it's inside the allowed area
}
最佳答案
尝试用自定义组件替换标题。在组件中,您可以将位置保持为状态,当位置更改时您会更新该状态。像这样:
class HomeScreen extends React.Component {
static navigationOptions = {
headerTitle: <LogoTitle />,
};
}
关于javascript - React-native - 基于用户位置的drawerLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582492/