0

In this code, authed state becomes true when the user logs in, but auth used in privateRoute does not become true according to the below code.

checkauth() {
  this.setState({
    authed:true
  })
}

render() {
  function PrivateRoute ({component: Component, auth, ...rest}) {
    return (
      <Route
        {...rest}
        render={(props) => auth === true
              ? <Component {...props} />
              : <Redirect to={{pathname: '/', state: {from: props.location}}} />}
      />
    )
  }

  return (
    <div>
      <Router history={browserHistory}>
        <div>
          <Navbar/>
          <Switch>
            <Route path="/"   component={Firstpage} exact > </Route>
            <Route path="/signup"   component={Signup}> </Route>
            <Route path="/shop/:id" component={Shop}></Route>
            <Route path="/login"  render={(props) => <Login {...props} checkauth= 
{this.checkauth.bind(this)} />}> </Route>
            <PrivateRoute path='/newpath' component={Welcome} auth= 
{this.state.authed}/> 
          </Switch>
        </div>
      </Router>
    </div>
  );
}

How can I use the states of the App component in the PrivateRoute function or is there any other method to do this?

c-chavez
  • 7,237
  • 5
  • 35
  • 49
Sandz
  • 99
  • 3
  • 12

0 Answers0