Manuel RODRIGUEZ
Manuel RODRIGUEZ

Reputation: 2161

Operate componentWillMount logic on a StackNavigator

I am trying to code a basic authentication screen with React Native.

Once the user is logged in it will be redirected to its home screen.

To do so I used a StackNavigator, as follows:

import React, { Component } from 'react';
import { StackNavigator } from 'react-navigation';

import LoginPage from './src/pages/login';

const MyApp = StackNavigator({
  Login: { screen: LoginPage },
  Home: { home: HomePage } 
});

export default MyApp;

But I would like to operate some logic here (such as DB init). As this is a pure component I am kind of lost...

How should I proceed? Can I "transform" the StackNavigator into a component with lifecycle functions for instance?

Upvotes: 3

Views: 216

Answers (1)

Val
Val

Reputation: 22797

You can make Stacknavigator object into React.Component this way:

const MyAppStack = StackNavigator({
  Login: { screen: LoginPage },
  Home: { home: HomePage } 
});

class MyApp extends Component {
  componentWillMount() {}
  componentWillUnmount() {}

  render() {
    return <MyAppStack />;
  }
}

Upvotes: 1

Related Questions