0

I have a react application.

i have problem in routing of component

index.js

import React from "react";
import ReactDOM from "react-dom";

import App from './app';

ReactDOM.render(
  <App/>,
  document.getElementById("root")
);

app.jsx

Here i am routing 2 component. Login and Main component. In Main component there are many router which will use for dashboard.

My Problem : In <Switch> the 1st <Route> can render but it's not rendering from 2nd router if i hardcode in url

http://localhost:3000/#/login == rendering

http://localhost:3000/#/main = Not rendering

import React, { Component } from 'react';
import {
    BrowserRouter as Router,
    Route,
    Link,
    Switch
} from 'react-router-dom';

import Login from './login';
import Main from './main';

import createBrowserHistory from 'history/createBrowserHistory';
const customHistory = createBrowserHistory();

class App extends Component {
    constructor(props) {
        super(props);

        window.token = '';
    }

    render() {
        return <div>
                <Router>
                    <Switch>
                        <Route to="/login" component={Login} exact />
                        <Route to="/main" component={Main} exact/>
                    </Switch>
                </Router>
            </div>;
    }
}

export default App;

main.jsx

import React, {Component} from 'react';
import ReactDOM from "react-dom";

import { HashRouter, Route, Switch } from "react-router-dom";

import indexRoutes from "routes/index.jsx";

import "bootstrap/dist/css/bootstrap.min.css";
import "./assets/css/animate.min.css";
import "./assets/sass/light-bootstrap-dashboard.css?v=1.2.0";
import "./assets/css/demo.css";
import "./assets/css/pe-icon-7-stroke.css";

import Login from './login';

class Main extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return <HashRouter>
        <Switch>

          {indexRoutes.map((prop, key) => {
            return <Route to={prop.path} component={prop.component} key={key} />;
          })}
        </Switch>
      </HashRouter>;
    }
}

export default Main;
4
  • What do you mean not rendering? what is happening? Commented Sep 17, 2018 at 7:00
  • what are inside the indexRoutes Commented Sep 17, 2018 at 7:01
  • @janaka inside indexRouter there are array of routers. It is not the problem. Commented Sep 17, 2018 at 7:08
  • If move Login component to 2nd then it'll not work but Main component will work Commented Sep 17, 2018 at 7:09

4 Answers 4

5

Use

<Route path="/login" component={Login} exact />

Instead of to use path

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

Comments

1

<Route path="/login" component={Login} /> <Route path="/main" component={Main} exact/>

Use Path insted of To

Comments

0

Don't use exact attribute in main Route as shown below.

class App extends Component {
  render() {
    return <div>
            <Router>
                <Switch>
                    ...
                    <Route to="/main" component={Main} />
                </Switch>
            </Router>
        </div>;
  }
}

Comments

0

There are two problems with your code

First in your app.jsx file:

<Switch>
  <Route 
   to="/login"       // <== should be path="/login"
   component={Login} 
   exact
   />
  <Route 
   to="/main"        // <== should be path="/main" 
   component={Main} 
   exact             // <== remove exact prop for the nested routes to work
   /> 
</Switch>

Here to prop actually belongs to the <Link/> component provided by react-router not the <Route /> component, It should be path prop.
If you want to render child routes within <Route /> component then we never use exact prop on the Parent <Route /> component.

Second in your main.jsx file:

  indexRoutes.map((prop, key) => {
    return (
     <Route 
       to={prop.path} // <== should be path={prop.path}
       component={prop.component} 
       key={key} />;
     );
  })

Again change to prop to path prop.

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.