1

I'm trying to get react router 4 to redirect to my login page that is publicly available when the user clicks the the logout button in their Navbar. The logout function works, the user is logged out, however, the redirect never happens. What am I missing here?

I'm using react-router v4 and meteorjs.

<button type="button" className="btn btn-link logout-button" onClick={() => Meteor.logout(() => (<Redirect to="/login" />))}>Logout</button>

bp123
  • 2,756
  • 4
  • 22
  • 53
  • 1
    You should render `Redirect` in order to make actual redirect. In your example you just return it from callback. Checkout [this SO question](https://stackoverflow.com/questions/42123261/programmatically-navigate-using-react-router-v4). Ctrl + F for '2.- Using withRouter HoC'. – Ramil Muratov Dec 04 '17 at 13:37

1 Answers1

2
import { Redirect } from "react-router";
class Header extends Component {
 constructor(props){
  super(props);

  this.state = {
    isLoggedIn: Meteor.userId(),
  }
 }

handleLogout(e){
e.preventDefault();

  Meteor.logout((error) =>{
    if (error) {
      console.log(error.reason);
    }else{
      this.setState({isLoggedIn: !this.state.isLoggedIn,});
      console.log("Logout successful");
     }
 });
}

render() {
 return (
   {
    (this.state.isLoggedIn) &&
      <Redirect to="/Login" />
    }
 );
}

Just do exactly like that it should redirect you.