-
-
Save rcanepa/b4ce0dff8d85b357504e04b03e69ac66 to your computer and use it in GitHub Desktop.
function PrivateRoute ({component: Component, authenticated, ...rest}) { | |
return ( | |
<Route | |
{...rest} | |
render={(props) => authenticated === true | |
? <Component {...props} /> | |
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />} | |
/> | |
) | |
} | |
<Route path='/' exact component={Home} /> | |
<Route path='/login' component={Login} /> | |
<Route path='/register' component={Register} /> | |
<PrivateRoute authenticated={this.state.authenticated} path='/invoices' component={Invoices} /> |
Hey guys, this question might sound weird but I am really very confused with this, so bear with me.
In the below code,
function PrivateRoute ({component: Component(-------Here we are using "C" but when we are passing the prop we are using "c" and if memory serves when destructuring the right side of the : gets its value assigned to the left side of the colon in the object and also in the bottom when we are typing the component we are also using "C" how and why is this working????---------- ) , authenticated, ...rest}) {
return (
<Route
{...rest}
render={(props) => authenticated === true
? <Component {...props} />(-------------Here we are using again "C"----------------------)
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>
)
}<Route path='/' exact component={Home} /> <Route path='/login' component={Login} /> <Route path='/register' component={Register} /> <PrivateRoute authenticated={this.state.authenticated} path='/invoices' component={Invoices} /> (-------------Here we are using again "c"----------------------)
Are my concepts wrong or is there some magic at play?
I have this same confusing. Can anyone help?
You can rename while destructuring thats what is happening there like so {actualName: newName},
so we receive component and rename it to be Component after desctucturing with {component: Component}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
Hello, I am having some issues with the React Router Dom PrivateRoute. The private route redirects to the login page even when the user is authenticated. I can't figure out the exact thing that I am doing wrong. Please see the routers and the privateRoute code below.
const PrivateRoute = ({ component: Component, authenticated, ...rest }) => (
<Route
{...rest}
render={props =>
authenticated ? (
<Component {...rest} {...props} />
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location }
}}
);