Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 213
irondsd Avatar asked Mar 05 '19 12:03

irondsd


1 Answers

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

      // ...
    },
  },
})
like image 101
Samuel Vaillant Avatar answered Sep 18 '22 01:09

Samuel Vaillant