Pass props through CreateAppContainer

irondsd picture irondsd · Mar 5, 2019 · Viewed 7.4k times · Source

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);

Answer

Samuel Vaillant picture Samuel Vaillant · Mar 5, 2019

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();

      // ...
    },
  },
})