How to navigate from login page to dashboard using react

This is my login page (App.jsx). I am trying to navigate to the control panel after a successful login. But this is not as expected. I want to know if it is possible to navigate from an external page (App.jsx) to a routing page (Dashboard.jsx).

import React from 'react';
import createHistory from 'history/createBrowserHistory';

const history = createHistory();

export default class App extends React.Component {

    constructor(props){
        super(props);
        this.state = {}
    }

    goToDashboard(e){
        this.props.history.push('/employeelist');
    }

    render() {
        return (
            <div>
                <form>
                    <div className="form-group">
                        <label htmlFor="email">Username:</label>
                        <input type="text" className="form-control" id="email" />
                    </div>
                    <div className="form-group">
                        <label htmlFor="pwd">Password:</label>
                        <input type="password" className="form-control" id="pwd" />
                    </div>
                    <button onClick={this.goToDashboard.bind(this)} className="btn btn-primary">Submit</button>
                </form>

            </div>
        );
    }
}

App.contextTypes = {
    router: React.PropTypes.object.isRequired
}

      

This is my dashboard page (Dashboard.jsx)

import React from 'react';
import App from './App.jsx';
import EmployeeRegister from './EmployeeRegister.jsx';
import EmployeeList from './EmployeeList.jsx';
import {HashRouter, Route, Link} from 'react-router-dom';

export default class Dashboard extends React.Component{

    render(){

        return(
            (
                <HashRouter>
            <div className="container">

                <nav className="navbar navbar-default">
                    <div className="container-fluid">
                        <div className="navbar-header">
                            <a className="navbar-brand" href="#">Employee</a>
                        </div>
                        <ul className="nav navbar-nav">
                            <li><Link to="/">Form</Link></li>
                            <li className="dropdown">
                                <a href="#" className="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Master Files <span className="caret"></span></a>
                                <ul className="dropdown-menu">
                                    <li><Link to="/employeereagister">CreateEmployee</Link></li>
                                </ul>
                            </li>
                            <li><Link to="/employeelist">Show Employees</Link></li>
                        </ul>
                    </div>
                </nav>


                <Route exact path="/" component={App}/>
                <Route exact path="/dashboard" component={Dashboard}/>
                <Route exact path="/employeereagister" component={EmployeeRegister}/>
                <Route exact path="/employeelist" component={EmployeeList}/>

            </div>
                </HashRouter>)
        );

    }

};

      

+3


source to share


1 answer


I would include everything in a higher order component (application) and do the routing inside the application component. This way it will be easier to structure your routes and change children as needed.

More or less like



<App>
  <Login />
  <Dashboard />
</App>

      

0


source







All Articles