"private routes in react" Code Answer's
You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "private routes in react" answered properly. Developers are finding an appropriate answer about private routes in react related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like private routes in react. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on private routes in react.
private route in react js
import React from 'react';
import {
BrowserRouter,
Route,
Switch,
Redirect
} from 'react-router-dom';
import decode from 'jwt-decode';
import Home from './Home';
import Register from './Register';
import Login from './Login';
import PrivateComponent1 from './PrivateComponent1';
import PrivateComponent2 from './PrivateComponent2';
const isAuthenticated = () => {
const token = localStorage.getItem('token');
try {
if(token){
return true;
}
else{
return false;
}
} catch (error) {
return false;
}
}
function PrivateRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={props =>
isAuthenticated() ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
}}
/>
)
}
/>
);
}
export default () => (
<BrowserRouter>
<Switch>
<Route path="/" exact component={Home}/>
<Route path="/register" exact component={Register}/>
<Route path="/login" exact component={Login}/>
<PrivateRoute path="/private1/:id?" exact component={PrivateComponent1}/>
<PrivateRoute path="/private2" exact component={PrivateComponent2}/>
</Switch>
</BrowserRouter>
);
private routes in react
import React from 'react'
import {Route, Redirect} from 'react-router-dom'
const PrivateRoute = ({
component: Component,
isAuthenticated,
userLevel,
...rest
}) => (
<Route {...rest} component={(props)=>
isAuthenticated ? (
<Component {...props} />
):(
<Redirect to="/login" />
)
} />
)
export default PrivateRoute
private route in react js
const PrivateRoute = ({component: Component, ...rest}) => {
const {isAuthenticated} = rest;
return (
<Route {...rest} render={props => (
isAuthenticated ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: {from: props.location}
}}/>
)
)}
/>
);
};
PrivateRoute.propTypes = {
isAuthenticated: PropTypes.bool.isRequired,
};
function mapStateToProps(state) {
return {
isAuthenticated: state.user.isAuthenticated,
};
}
export default connect(mapStateToProps)(PrivateRoute);
Source: stackoverflow.com
All those coders who are working on the Javascript based application and are stuck on private routes in react can get a collection of related answers to their query. Programmers need to enter their query on private routes in react related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about private routes in react for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of private routes in react while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "private routes in react". Visit this developer's friendly online web community, CodeProZone, and get your queries like private routes in react resolved professionally and stay updated to the latest Javascript updates.