【问题标题】:componentWillMount function in React Native NavigationReact Native Navigation 中的 componentWillMount 函数
【发布时间】:2017-06-15 08:48:36
【问题描述】:

我是 Ract Native 的新手,我正在尝试使用 react native 导航构建一个小应用程序。

我可以在他们网站上提供的电影应用示例(反应原生导航)中看到,他们没有在 app.js 中使用 componentWillMount 函数,而是在构造函数中调用了 startApp() 函数。

我基本上是想在 componentWillMounth 函数中定义 firebase 配置,但是一个简单的控制台日志显示该函数不知何故永远不会运行。

我想正确设置应用程序,以获得最佳性能,所以我想知道使用 startApp 功能是否基本相同,不需要使用 componentWillMount 功能?

更新:以下代码

index.ios.js

import App from './src/app'; const app = new App();

app.js

import ...

class App extends Component {
    state = {
      loggedIn: null
    }

  constructor(props) {
    super(props);
    this.startApp();
  }

  componentWillMount() {
    console.log('COMPONENT WILL MOUNT');
    // Initialize Firebase
    const config = {
      ...
      ...
    };
    firebase.initializeApp(config);
  }


  startApp() {
    console.log('START APP');

    // Initialize Firebase here???




const tabs = [
     ...
];

  if (this.state.loggedIn) {
    Navigation.startTabBasedApp({
      tabs,
      tabsStyle: {
        ...
      }
    });
  } else {
    Navigation.startSingleScreenApp({
      ...
    });
  }
  } // startApp
}
export default App;

【问题讨论】:

  • 请分享您的代码的相关部分,否则很难找出确切的问题。此外,您还写了“componentWillMounth”。检查代码中是否没有相同的错字,因为如果是,那就是它没有被调用的原因。
  • 嗨@mar​​tinarroyo,现在没有太多事情发生,我只是想找出初始化firebase配置的最佳位置。无论如何,我添加了一些简化的代码以使其更清晰,希望对您有所帮助。

标签: firebase react-native react-native-navigation


【解决方案1】:

您的 componentWillMount 没有被执行,因为您没有在 React 渲染生命周期内使用您的组件(您不是在 render 中实例化它,而是在 new 中实例化它,这只会运行组件的构造函数。移动将它添加到您的索引渲染方法中,如下所示:

class EntryPoint extends React.Component {
  render(){
    <App />
  }
}

AppRegistry.registerComponent('MyApp', ()=>EntryPoint);

【讨论】:

  • 谢谢@martinarroyo。不幸的是,这个解决方案不起作用。不返回错误,但屏幕上不显示任何内容。我还添加了缺少的 return() 部分,但没有运气。我想知道我目前使用的解决方案是完全错误的,还是“只是另一种方式”?
  • 也许你的代码有一些小错误,如果你发布完整的 JS 文件,我们可能会发现它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多