1

I want to pass handleLogin in App.js to home.js however when I do I get the error: [TypeError: this.props.extraData.handleLogin is not a function. (In 'this.props.extraData.handleLogin(data)', 'this.props.extraData.handleLogin' is undefined)] How do I properly pass a function in stack navigator so I can use it in the props in another component? I think I'm missing something with the this context

App.js:

export default class App extends Component {
  constructor() {
    super();

    this.state = {
      loggedInStatus: "NOT_LOGGED_IN",
      user: {}
    };
    console.log("APP PROPS", this)
    this.handleLogin = this.handleLogin.bind(this);
    this.handleLogout = this.handleLogout.bind(this);
  }


handleLogin(data) {
    this.setState({
      loggedInStatus: "LOGGED_IN",
      user: data.user
    });
  }

render() {
    return (
    <NavigationContainer>
       <Stack.Navigator>
       <Stack.Screen name="Home" >

       {props => <Home {...props} extraData={this.handleLogin}/>}
        </Stack.Screen>
        <Stack.Screen name= "Login" >
        {props =>  <Login {...props}/>}
        </Stack.Screen>
        <Stack.Screen name="Registration">
         {props =>  <Registration {...props} extraData={this.handleLogin, this.handleLogout, this.loggedInStatus}/>}

         </Stack.Screen>
       </Stack.Navigator>
     </NavigationContainer>
    );
  }
}

Home.js:

export default class Home extends Component {
  constructor(props) {
    super(props);
    console.log("HOME PROPS", this.props)
    this.handleSuccessfulAuth = this.handleSuccessfulAuth.bind(this);
    this.handleLogoutClick = this.handleLogoutClick.bind(this);
  }

  handleSuccessfulAuth(data) {
    this.props.extraData.handleLogin(data);
    this.props.history.push("/dashboard");
  }
2
  • 2
    Have you defined handleLogin? You haven't in the code you provided. Commented Mar 18, 2021 at 5:21
  • ah sorry I overlooked that I made an edit, thank you Commented Mar 18, 2021 at 17:29

2 Answers 2

1

Seems you failed to pass a proper extraData prop object. The object should have keys that match the values you pass, i.e. for this.props.extraData.handleLogin the extraData object should have a handleLogin property.

<Stack.Screen name="Home" >
  {props => (
    <Home
      {...props}
      extraData={{ handleLogin: this.handleLogin }}
    />
  )}
</Stack.Screen>
...
<Stack.Screen name="Registration">
  {props =>  (
    <Registration
      {...props}
      extraData={{
        handleLogin: this.handleLogin,
        handleLogout: this.handleLogout,
        loggedInStatus: this.loggedInStatus,
      }}
    />
  )}
</Stack.Screen>

NOTE: Missing from your App component snippet are these function definitions, so make sure those are all defined.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thank you! This worked, didn't even realize I was passing them wrong, I was treating the value I passed as a dictionary
1

You are sending props wrong:

App.js

<Stack.Screen name="Registration">
  {(props) => (
    <Registration
      {...props}
      extraData={{
        handleLogin: this.handleLogin,
        handleLogout: this.handleLogout,
        loggedInStatus: this.loggedInStatus,
      }}
    />
  )}
</Stack.Screen>;

Also be sure you have defined those functions that you are sending to Home.js are defined in App.js.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.