javascript - React-native - 基于用户位置的drawerLabel

标签 javascript reactjs typescript react-native asynchronous

我有一个 react-native 应用程序,我需要根据用户位置呈现一个菜单项,但是返回用户位置的函数是一个异步函数,我得到一个未定义的,所以我的问题是如何我根据用户位置设置 navigationOptionsdrawerLabel 值?

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 />,
  };
}

Read more here

关于javascript - React-native - 基于用户位置的drawerLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582492/

相关文章:

javascript - 错误 : there is no parameter $1

javascript - 如何仅在使用 jquery 扩展的文本区域上调用 ajax?

javascript - react-beautiful-dnd 拖放区宽度不一致

reactjs - React Flux - 从通量调度程序/存储返回值

javascript - Ant 设计表单initialValue如何使用

javascript - 在 Angular 2 App 中转换双向绑定(bind)属性中的日期

Angular 单元测试 : Using Generics to Create Component

javascript - 将一个组件的值传递给另一个 Angular

javascript - 使用 firebug 在 firefox 中调试 coffeescript

javascript - Javascript中的自定义方法实现模式