Redirect on Login and Logout

From @jayair on Mon Apr 10 2017 01:04:21 GMT+0000 (UTC)

Link to chapter - http://serverless-stack.com/chapters/redirect-on-login-and-logout.html

Copied from original issue: https://github.com/AnomalyInnovations/serverless-stack-com/issues/42

From @deepseafishing on Sun Jul 30 2017 23:54:29 GMT+0000 (UTC)

Hi, @jayair.

I was wondering if I have misunderstanding in history props.

I thought when we use , it’s the component that handles down history props to react componetns and had nothing to do with withRouter. I thought using withRouter is to recognize changes in the url and rerenders react component.

Am I getting this wrong?

From @jayair on Mon Jul 31 2017 01:20:57 GMT+0000 (UTC)

@deepseafishing I’m not sure if I completely get your question but there are a couple of ways to do redirects with React Router v4. We use two of these in our app; use the history prop or use the <Redirect /> component. We are using the history prop here. Are you wondering if we should not use the history prop here?

From @deepseafishing on Mon Jul 31 2017 01:27:00 GMT+0000 (UTC)

""“This Higher-Order Component adds the history prop to our component.”""

It was this quote that was confusing me. That HOC (which you meant for withRouter) doesn’t add the history prop to our component right? I thought it was the Route Component that gives you history prop to our component. I thought withRouter only informs about the change in url to react component so that it can rerender.

From @jayair on Mon Jul 31 2017 01:51:30 GMT+0000 (UTC)

@deepseafishing I see what you are saying. The withRouter HOC also gives you the history props. We need this in cases where our component isn’t rendered by a <Route />. An example of this is the App component.

But in this case you are right. It is redundant having the withRouter. The history prop should already be available (here and in all our containers). I’ll have to test it and update the chapters.

From @deepseafishing on Mon Jul 31 2017 01:57:05 GMT+0000 (UTC)

@jayair
Great! your answer made everything so clear. Thanks You, Jayair! I’m getting so much help from this tutorial.

FYI: The setup installs a backlevel version of react-router-dom (5.0.1) which does not contain use-history. I had to update the setting in package.json to “react-router-dom”: “^5.1.2”, which does contain ‘use-history’.

Yeah we updated the guide recently to the latest React Router version.

Hi there!
The tutorial says that
"initialize useHistory hook in the beginning of src/containers/Login.js"
but it is not going to work.
We should initialize it after export default function Login() {
Please, fix that!

Yup, added!