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

TypeError: Object(...)(...) is undefined in AuthRoute #14

Closed
aindriu80 opened this issue Nov 5, 2020 · 3 comments
Closed

TypeError: Object(...)(...) is undefined in AuthRoute #14

aindriu80 opened this issue Nov 5, 2020 · 3 comments

Comments

@aindriu80
Copy link

AuthRoute.js
`
import React, { useContext } from "react";
import { Route, Redirect } from "react-router-dom";

import { AuthContext } from "../context/auth";

function AuthRoute({ component: Component, ...rest }) {
const { user } = useContext({ AuthContext });

return (
<Route
{...rest}
render={(props) =>
user ? : <Component {...props} />
}
/>
);
}
export default AuthRoute;
`

Gets the following Error:

TypeError: Object(...)(...) is undefined

@mariusflorescu
Copy link

Hello @aindriu80 !

At line 7:
const { user } = useContext({ AuthContext });
Should be changed to:
const { user } = useContext(AuthContext); //delete the curly braces {}

And since you're working on the Auth Route , you should be redirecting any user that attempts to go to routes such as "/login" or "/register" to the home page.

That being said at line 13 you should change from:
user ? : <Component {...props} />
to:
user ? <Redirect to="/"/> : <Component {...props}/>

So, the AuthRoute.js should look like this:

import React, {useContext} from 'react'
import {Route,Redirect} from 'react-router-dom'

import {AuthContext} from './context/auth'

function AuthRoute({component: Component, ...rest}){
  const {user} = useContext(AuthContext);
  
  return (
    <Route 
      {...rest}
      render={props => 
        user ? <Redirect to="/"/> : <Component {...props}/>
      }/>
  )
}

export default AuthRoute

@aindriu80
Copy link
Author

That worked a treat, thanks!!

@mariusflorescu
Copy link

Your welcome,@aindriu80!
You can mark the issue as closed 😁.

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

2 participants