3

I have just started using React and have created a new project using create-react-app. I have made a homepage which works. However, I want to add another page to this application. I have a button on the homepage which I want to redirect to another page when clicked. Can someone please give a simple explanation to do this?

I do know that there are other questions that ask this, but they don't seem to apply to create-react-app, or I don't understand them since I am just starting out with React.

Here is my index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Route, BrowserRouter as Router } from 'react-router-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render((
    <Router>
        <Route path = "/" component = {App} />
    </Router>
), document.getElementById('root'));
registerServiceWorker();

2 Answers 2

2

You need to use react-router-dom library. Navigation between pages is really easy in react.

import React from 'react';
import {render} from 'react-dom';
import {Router, Route, hashHistory, BrowserRouter} from 'react-router-dom'

render((
         <BrowserRouter basename="/" history={hashHistory}>
           {routes()}
         </BrowserRouter>
       ), document.getElementById('app'));

Then in your home page, you can have something similar to below page:

import React, { Component } from 'react';
import { Switch, Route, BrowserRouter, hashHistory } from 'react-router-dom';
import './MainLayout.css';
import ReportNavComponent from './ReportNavComponent';
import ListOfUsers from '../reports/ListOfUsers/ListOfUsers';
import ListOfProviders from '../reports/ListOfProviders/ListOfProviders';
import ListOfDiagnosis from '../reports/ListOfDiagnosis/ListOfDiagnosis';
import ListOfNewPatients from '../reports/ListOfNewPatients/ListOfNewPatients';
import NumberOfAdmissions from '../reports/NumberOfAdmissions/NumberOfAdmissions';

...

<div className="reportContent">
                        <Switch>
                            <Route path="/ListOfUsers" component={ListOfUsers} />
                            <Route path="/ListOfProviders" component={ListOfProviders} />
                            <Route path="/ListOfDiagnosis" component={ListOfDiagnosis} />
                            <Route path="/ListOfNewPatients" component={ListOfNewPatients} />
                            <Route path="/NumberOfAdmissions" component={NumberOfAdmissions} />
                            <Route component={ListOfUsers} />
                        </Switch>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It worked! However, it works when I don't import hashHistory and provide it to BrowserRouter. Apparently, hashHistory is not in react-router-dom... Am I using the wrong version?
When I try to navigate from one page to another, the url changes, but the page doesn't change... what am I doing wrong?
1

reacter-router-dom is the way to go about routing. But if you have just started with react, why don't start with a boiler-plate project and see how the components and other entities interact?

You should definitely look at davezuko's react-redux starter kit and experiment with it.

1 Comment

Ruta3 is another routing lib I use alternatively.

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.