score:4
Accepted answer
for the /
route you should use render like this:
<route exact path="/" render={() => {
if (auth.isuserauthenticated()) {
(<dashboardpage)/>)
} else {
(<homepage/>)
}
}} />
for your /logout
route using <redirect>
<route path="/logout" render={() => {
auth.deauthenticateuser();
return <redirect to={{ pathname: "/login" }} />;
}}
/>
score:2
i post a second solution for the /logout path using withrouter and this.props.history.push('/login').
- point the /logout route to its own component "logout"
- wrap the logout component with withrouter method from react-router-dom.
logout.js
import react, { component } from "react";
import auth from "../modules/auth";
import { withrouter } from "react-router-dom";
class logout extends component {
constructor(props) {
super(props);
this.handlelogout = this.handlelogout.bind(this);
}
handlelogout() {
auth.deauthenticateuser();
this.props.history.push("/login");
}
render() {
return (
<div>
{this.handlelogout()}
</div>
);
}
}
export default withrouter(logout);
Source: stackoverflow.com
Related Query
- TypeError when using React: Cannot read property 'firstChild' of undefined
- Cannot read property 'push' of undefined for react use history
- TypeError Cannot read property 'price' of undefined
- Gatsby TypeError - Cannot read property 'id' of undefined
- Cannot read property push of undefined for react use history
- TypeError cannot read property url of undefined on strapi
- React-Redux mapStateToProps TypeError Cannot read property '' of undefined
- TypeError Cannot read property 'find' of undefined
- React Facebook Login with Enzyme mount - TypeError cannot read property 'parentNode' of undefined
- TypeError Cannot read property 'baseTheme' of undefined
- cannot read property history because it's undefined but it is
- React : TypeError : Cannot read property 'fullname' of undefined
- React js and apexcharts Unhandled Rejection TypeError Cannot read property 'length' of undefined
- React - uncaught TypeError: Cannot read property 'setState' of undefined
- Line 0: Parsing error: Cannot read property 'map' of undefined
- react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined
- Error : Cannot read property 'map' of undefined
- React with ES7: Uncaught TypeError: Cannot read property 'state' of undefined
- Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)
- React enzyme testing, Cannot read property 'have' of undefined
- Cannot read property 'history' of undefined (useHistory hook of React Router 5)
- React - TypeError: Cannot read property 'props' of undefined
- React-Router - Uncaught TypeError: Cannot read property 'getCurrentLocation' of undefined
- Redux TypeError: Cannot read property 'apply' of undefined
- React Router: Cannot read property 'pathname' of undefined
- React Typescript: Line 0: Parsing error: Cannot read property 'name' of undefined
- reactjs Cannot read property 'keys' of undefined
- TypeError: Cannot read property 'contextTypes' of undefined
- TypeError: Cannot read property 'prepareStyles' of undefined
- Cannot read property '.then' of undefined when testing async action creators with redux and react
More Query from same tag
- Webpack-dev-server showing directory list instead of the app page
- How to have a local instance or React hit a local instance of Django
- Unregistering e.preventDefault in reactJS method
- How to use Context API to pass down a state while using a Router in React JS
- How to not call any api endpoints if firebase token is expired in React app?
- How to implement fifo queue with Reactjs&Redux
- How do I dynamically display an icon for each category with React?
- Trying to create a a reveal footer in React, footer ignoring other divs
- Socket.io not emitting the current state of an array to client side
- undefined method `map' for "1,2":String
- Cannot access 'actionCreator' before initialization
- beginner at react.js - rendering component from state which is updated later
- reactjs custom component input value doesnt update with state change
- Testing components that contain react router Link with Enzyme
- setState doesn't work when using computed property names
- Why don't the comments appear instantly in my React app project?
- Work on ReactJS that was developed on Linux/Mac on Windows
- TypeError: Object(...) is not a function - I am lost
- jest.requireActual is not a function
- What is the purpose of "additionalItems" property on arrays in rjsf?
- react.js does not update DOM after having changed state array
- × TypeError: Cannot read property 'map' of undefined
- How to make react bootstrap tooltip to fit into container?
- add custom property to props.location
- How to get exif data from URL in react?
- How to override Drawer in Material UI
- .keyboard is not a function when used with React
- How to remove overflowX during transition
- How to add onclick event for info boxes into pushpins on bingmaps in reactjs
- React - axios post request with body