1

I split my routes into two files.

const masterRoutes=(

                      <Switch>
                      <Route path="/diamond"  component={DiamondMaster}   />
                      <Route path="/gram"     component={GramMaster}      />
                      </Switch>

                    )

export default masterRoutes; 



const userRoutes=(

                      <Switch>
                      <Route path="/user"  component={User}   />
                      <Route path="/store" component={Store} />
                      <Route path="/userlist" component={UserList} />
                      </Switch>

)



export default userRoutes;

app.jsx is the main file handling all routes.

export class App extends Component {
  render() {
    return (

          <div>


                  <Router history={history}  >
                      <div>
                      <AuthHeader isLoggedIn={true} />
                      <Route path="/" component={AuthHeader} />                                 
                      <Switch>
                      <Route path="/login" component={Login} />
                      <Route path="/tab" component={SettingsComp} />
                       {userRoutes}
                       {masterRoutes}
                     </Switch>
                      </div>
                  </Router>
            </div>
    );
  }

It is working fine when i click user component But MasterRoutes is not working.

App.jsx file doesn't take MasterRoutes file. Is there any other way to implement this?

I am bit new to reactjs. Please bear with my question.

1 Answer 1

3

What is happening is the first Switch statement is taking precedence. If you swap them around, then master works, and user doesn't.

I'm not exactly sure what the goal is but if you want the list of routes to be extracted, you can instead do this:

const masterRoutes=[
  <Route path="/diamond"  component={DiamondMaster}   />
  <Route path="/gram"     component={GramMaster}      />
];

const userRoutes=[
  <Route path="/user"  component={User}   />
  <Route path="/store" component={Store} />
  <Route path="/userlist" component={UserList} />
];

Instead of each of them having their own Switch, they instead are just arrays of Routes. So when you add them to the Switch tag you already have, you are just extending the number of routes to include those from userRoutes and masterRoutes.

All of your other code should be able to stay the same. Exporting, importing and adding them to the router {masterRoutes} {userRoutes}.

If you want child routes, that would handled differently.

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.