Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Your guide doesn't match your demo #1

Open
samjross opened this issue Dec 10, 2019 · 0 comments
Open

Your guide doesn't match your demo #1

samjross opened this issue Dec 10, 2019 · 0 comments

Comments

@samjross
Copy link

Reading this guide here
https://www.sitepoint.com/react-router-v4-complete-guide/

I was following it word for word, doing everything perfectly, up until the section about Protected Routes.

You say to put this in app.js

<Switch>
  <Route exact path="/" component={Home} data={data}/>
  <Route path="/category" component={Category}/>
  <Route path="/login" component={Login}/>
  <PrivateRoute authed={fakeAuth.isAuthenticated} path='/products' component = {Products} />
</Switch>

With this as the private route definition:

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

And.. I won't bother describing the Login component, it's what you put on the article.

Anyway, as its described in the article, it doesn't actually work. The log in button on the Login route doesn't actually force the App.js route to refresh or to change the value that it's passing as authed prop to the PrivateRoute, so the redirect doesn't work when someone follows your guide.

So I downloaded the github code here and the code is drastically different, calculating the authed status inside the PrivateRoute rather than passing it in as a prop. And that works!

Your article was good and informative, so I thought I'd contribute by helping you fix this small issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant