0

I'm trying to solve issue with redirection in react-router v4. I want to redirect to Index component if user is logged in and when load, reload show CodeClient component when user is logged out.

I never use react-router-dom, how can do it?

Somebody can explain me and show me with code how do it?

Here is my code:

import React, { Component } from 'react';

import './App.css';
import CodeClient from './components/Login/CodeClient';
import IndexComponent from './components/Index/IndexComponent';

class App extends Component {

    constructor(props) {
        super(props);
        this.state = { user: undefined, logged: false };
    }

    doParentCode = (user) => {
        // when user is different of undefined
        this.setState({ logged:true, user: user});
    }

    render() {

        return (
            <div className="Loggin">
                {
                    (!this.state.logged) ? <CodeClient parentCode={this.doParentCode} /> : <IndexComponent user={this.state.user}/>
                }
            </div>
        );
    }
}

export default App;
Javier
  • 1,975
  • 3
  • 24
  • 46
  • Thanks a lot for your info but, I'm trying to make it and I can't. I dont understand it. Cold you show me how make it with my code? – Javier Jun 13 '18 at 13:00
  • Sure, I would go for something along the lines of [this sandbox](https://codesandbox.io/s/xl6o10lw6p). – Arman Charan Jun 13 '18 at 13:15

0 Answers0