在应用加载和绑定导航器时调用函数



为了上帝的爱,我找不到任何东西,或者我只是不知道该搜索什么。我是 React 和 React Native 的新手,我需要 RN 项目的帮助。我正在尝试做的是调用我的组件MainLogic中的函数auto_login()。该函数如下所示,并放置在module.exports中:

auto_login: function(navigator){
    if(GLOBALS.LOGGED_IN != true){
      Keychain
      .getGenericPassword()
      .then(function(credentials){
        fetch(GLOBALS.API_URL, {
          method: "POST",
          headers: {
            'Accept': 'application/x-www-form-urlencoded',
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: "username=" + credentials.username + "&password=" + credentials.password,
        })
        .then((response) => response.json())
        .then((response) => {
          if(JSON.stringify(response.status).replace(new RegExp('"', 'g'), '').match("OK")){
            GLOBALS.LOGGED_IN = true;
            GLOBALS.USERNAME = credentials.username;
            GLOBALS.PASSWORD = credentials.password;
            navigator.push({
              id: 'news'
            });
          }
        })
        .catch((e) => {
          console.warn(e);
        })
        .done();
      });
    }
  },

现在我正在从

componentDidMount: function(){
  MainLogic.auto_login(navigator);
},

render功能之前,但我意识到导航器脱离了上下文或其他什么。我需要一些帮助来理解我将如何调用此函数并正确绑定navigator。我现在收到警告:

TypeError: navigator.push is not a function. (in 'navigator.push({id:'news'})', 'navigator.push' is undefined.
任何

有助于理解这一点的帮助,非常感谢!

编辑:我正在使用React.createClass。

由于您尝试替换文件中main_logic.js导航器,因此auto_login中的此关键字没有指向函数的上下文。绑定也无济于事,因为关键字仍然指向相同的上下文。

我能想到的最好的方法是将上下文(这个关键字)传递给main_logic.js,然后而不是调用

this.refs['nav']

我们将它更改为

paramName.refs['nav']

我怀疑可能有更好的解决方案,所以我鼓励你在 React Native 和 JS 中前进时更深入地研究并迭代问题。

最新更新