0

I am new to React and I am trying to display a react route using react router dom but the matching component is not coming up. I have looked at answers from other OS questions related but no help. Here is my app.js file

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import AdminLogin from './AdminLogin';


export default class AdminApp
 extends Component {
   render() {
     return (
        <Router>
            <Switch>
                <div className="App">
                        <Route path="/" exact={true}  component = {AdminLogin} />
                </div>
            </Switch>
        </Router>
    );
}
}

if (document.getElementById('app')) {
    ReactDOM.render(<AdminApp />, document.getElementById('app'));
}

When i inspect my html file i get this

<div id="app">
    <div class="App" location="[object Object]" computedmatch="[object Object]"></div>
</div>

this is my adminLogin component

import React, {Component} from 'react';
import {adminLogin} from './UserFunctions';

export default class AdminLogin extends Component {
    constructor(){
    super()
    this.state = {
        email: '',
        password: '',
        errors: {}
    }
    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
}

onChange(e){
    this.setState({ [e.target.name]: e.target.value })
}

onSubmit(e){
    e.preventDefault;
    const user = {
        email : this.state.email,
        password : this.state.password
    }

    adminLogin(user).then(res => {
        if(res) {
            this.props.history.push('/dashboard');
        }
    })
}


render(){
    return (
        <div className="kt-grid kt-grid--ver kt-grid--root">
            <div className="kt-grid kt-grid--hor kt-grid--root  kt-login>
                  <h1>Login Now</h1>
            </div>
        </div>
    )
}

}
12
  • Can you share your AdminLogin component? Commented Sep 29, 2019 at 15:37
  • Seems like your routing is correct. Please see this sample I put together with your code. codesandbox.io/s/serene-river-jypnl Commented Sep 29, 2019 at 15:40
  • @UdithGunaratna i ran your code and it did not work. Commented Sep 29, 2019 at 15:53
  • There is an error in your AdminLogin component. You haven't closed the className string of inner div element. It should be <div className="kt-grid kt-grid--hor kt-grid--root kt-login"> Commented Sep 29, 2019 at 16:03
  • I edited my code and removed all the tags and just returned hello in my adminLogin and nothing outputs still Commented Sep 29, 2019 at 16:07

1 Answer 1

1

This only works if you are trying the root path (/). If you need to render this component at a subpath (e.g. /foo/bar/login), that should be defined as the route path.

<Route path="/foo/bar/login" exact={true} component = {AdminLogin} />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.