irondsd
irondsd

Reputation: 1240

Pass props through CreateAppContainer

I'm trying to pass props through AppContainer. I was able to pass in through other components, but I can't figure out how to send props through createAppContainer

in App.js:

render() {
    return (
        this.state.isLoggedIn ? <DrawerNavigator /> : 
<SignedOutNavigator handler={this.saveUserSettings} />
    )
}

in SignedOutNavigator:

import React from "react";
import { View, Text } from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import LoginScreen from "../screens/LoginScreen";

const SignedOutNavigator = createStackNavigator({
    Login: {
        // screen: LoginScreen
        screen: props => <LoginScreen screenProps={value => {
            // I need to access props from here
            // like this.props.handler(value)
        }} />,
        navigationOptions: ({ navigation }) => ({
            header: null,
        }),
    }
});

export default createAppContainer(SignedOutNavigator);

Upvotes: 8

Views: 8571

Answers (2)

irondsd
irondsd

Reputation: 1240

Okay, I got this to work with help of Samuel Vaillant. I had to make couple of modifications.

My App.js:

render() {
        return (
            this.state.isLoggedIn ? <DrawerNavigator /> : <SignedOutNavigator
                screenProps={{
                    handler: (settings) => { this.saveUserSettings(settings) }
                }}
            />
        )
    }

My SignedOutNavigator:

import React from "react";
import { View, Text } from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import LoginScreen from "../screens/LoginScreen";

const SignedOutNavigator = createStackNavigator({
    Login: {
        // screen: LoginScreen
        screen: screenProps => <LoginScreen screenProps={value => {
            screenProps.screenProps.handler(value)
        }} />,
        navigationOptions: ({ navigation }) => ({
            header: null,
        }),
    }
});

export default createAppContainer(SignedOutNavigator);

Upvotes: 2

Samuel Vaillant
Samuel Vaillant

Reputation: 3847

You have to scope the props under screenProps to be able to access it at the screen level.

// App.js
<AppNavigator
  screenProps={{
    handler: () => {},
    hello: "World"
  }}
/>

// Navigator.js
const StackNavigator = createStackNavigator({
  Login: {
    screen: ({ screenProps, navigation }) => {
      screenProps.handler();

      // ...
    },
  },
})

Upvotes: 10

Related Questions