# Creating a Single-Page App in React using React Router

**URL:** https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201
**Category:** programming
**Created:** [March 20, 2017, 1:42am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201 "2017-03-20T01:42:15Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [March 20, 2017, 1:42am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/1 "2017-03-20T01:42:15Z")

</div>

by [kirupa](http://www.kirupa.com/www.kirupa.com/me/index.htm) | 5 March 2017

Now that you've familiarized yourself with the basics of how to work with React, let's kick things up a few notches. What we are going to do is use React to build a simple **single-page app** (also referred to as **SPA** by the cool kids...and people living in Scandinavia). Like we talked about in our [React introduction](https://www.kirupa.com/react/introducing_react.htm) forever ago, single-page apps are different from the more traditional multi-page apps that you see everywhere. The biggest difference is that navigating a single-page app doesn't involve going to an entirely new page. Instead, your pages (commonly known as **_views_** in this context) typically load inline within the same page itself:

* * *
This is a companion discussion topic for the original entry at [http://www.kirupa.com/react/creating\_single\_page\_app\_react\_using\_react\_router.htm](http://www.kirupa.com/react/creating_single_page_app_react_using_react_router.htm)

---

<div class="post-metadata">

### Author: ![balaMurgan](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@balaMurgan](https://forum.kirupa.com/u/balaMurgan)
#### Post date: [April 18, 2017, 6:31am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/2 "2017-04-18T06:31:39Z")

</div>

Hi, waht changes do I need to make to the code to get it running on react router 4.1.1?

Please help, as I am stuck on this since days.

Thanx,  
Bala

---

<div class="post-metadata">

### Author: ![balaMurgan](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@balaMurgan](https://forum.kirupa.com/u/balaMurgan)
#### Post date: [April 18, 2017, 6:39am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/3 "2017-04-18T06:39:53Z")

</div>

Could you pls mention the github link for this code.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 18, 2017, 6:00pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/4 "2017-04-18T18:00:40Z")

</div>

I haven’t had a chance to convert this example into React Router 4.1.1 yet, but I’ll start fiddling with that shortly and post an update here once that has been done 🙂

---

<div class="post-metadata">

### Author: ![balaMurgan](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@balaMurgan](https://forum.kirupa.com/u/balaMurgan)
#### Post date: [April 19, 2017, 12:36am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/5 "2017-04-19T00:36:03Z")

</div>

Thanx for the quick revert. Eagerly waiting for it.  
Request you to provide it ASAP.

---

<div class="post-metadata">

### Author: ![sctm81](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sctm81/32/8407_2.png) [@sctm81](https://forum.kirupa.com/u/sctm81)
#### Post date: [April 21, 2017, 7:45am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/6 "2017-04-21T07:45:54Z")

</div>

Yes, please update ASAP. I purchased your book and couldn’t progress as this critical part isn’t working.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 21, 2017, 4:28pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/7 "2017-04-21T16:28:16Z")

</div>

You can use the older version of React Router to get familiar with how it works, but updating it is on my to-do list 🙂

---

<div class="post-metadata">

### Author: ![sctm81](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sctm81/32/8407_2.png) [@sctm81](https://forum.kirupa.com/u/sctm81)
#### Post date: [April 24, 2017, 11:21pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/8 "2017-04-24T23:21:17Z")

</div>

Could you please explain how to do use the ‘older version’ of React Router? I’m just stuck trying follow the example in the book. Thanks

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 24, 2017, 11:34pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/9 "2017-04-24T23:34:21Z")

</div>

There is a typo in the React book currently. Please include this file:

`<script src="https://npmcdn.com/react-router@2.4.0/umd/ReactRouter.min.js"></script>`

The book doesn’t have the “2.4.0” version, and that causes the latest version to be incorrectly loaded. It’s been on the book’s hard-to-find errata page, so apologies for not spotting this as the problem earlier 🙂

---

<div class="post-metadata">

### Author: ![sctm81](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sctm81/32/8407_2.png) [@sctm81](https://forum.kirupa.com/u/sctm81)
#### Post date: [April 24, 2017, 11:41pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/10 "2017-04-24T23:41:53Z")

</div>

Thanks for the quick reply. Yes, that works now.

---

<div class="post-metadata">

### Author: ![janh](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@janh](https://forum.kirupa.com/u/janh)
#### Post date: [May 1, 2017, 6:18am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/11 "2017-05-01T06:18:51Z")

</div>

I got the example in the tutorial working with react-router version 3.0.5 (loaded as an npm module). There are apparently a lot of breaking changes in version 4. In fact, it is described in various posts as a complete rewrite. Among other things, nesting of routes does not make the components associated with the child routes implicit “children” of the component that is associated with the parent route. As a result, the sample application does not work as-is with version 4.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [May 1, 2017, 11:27pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/12 "2017-05-01T23:27:52Z")

</div>

Yes - the breaking changes are quite numerous. I’m in the process of re-writing that article right now, so expect an updated version in a few days (barring any unforeseen delays) 🙂

---

<div class="post-metadata">

### Author: ![criszama](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@criszama](https://forum.kirupa.com/u/criszama)
#### Post date: [May 19, 2017, 8:11am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/13 "2017-05-19T08:11:39Z")

</div>

Hello  
thanks for this tutorial but i need a responsive menu …  
someone do you have a tutorial to make a navbar react-bootstrap without webpack?  
thanks a lot

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [November 5, 2017, 8:45pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/14 "2017-11-05T20:45:49Z")

</div>

I just updated this tutorial to work with React Router 4 and create-react-app! Sorry for the massive delay on that, everyone 🙂

---

<div class="post-metadata">

### Author: ![yogita.rjpl](https://avatars.discourse-cdn.com/v4/letter/y/dbc845/32.png) [@yogita.rjpl](https://forum.kirupa.com/u/yogita.rjpl)
#### Post date: [November 22, 2017, 6:11pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/15 "2017-11-22T18:11:16Z")

</div>

Hi kirupa, I followed your tutorial to create navigation in my project, But as soon as i introduced router in code. It fails with this exception  
Stack: Invariant Violation: Hash history needs a DOM

It seems some imports are missing in your tutorial.

---

<div class="post-metadata">

### Author: ![arol](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arol/32/8862_2.png) [@arol](https://forum.kirupa.com/u/arol)
#### Post date: [December 14, 2017, 10:07am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/16 "2017-12-14T10:07:23Z")

</div>

When we run npm start the default url is localhost:3000.

I was wondering if I can use my own domain dev instead of localhost without running nmp run build

Could you help me with this?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 16, 2017, 8:55pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/18 "2017-12-16T20:55:35Z")

</div>

Does this help? [https://github.com/facebookincubator/create-react-app/issues/1959](https://github.com/facebookincubator/create-react-app/issues/1959)

🙂

---

<div class="post-metadata">

### Author: ![Tirumalesh\_Chinna](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tirumalesh_chinna/32/8886_2.png) [@Tirumalesh\_Chinna](https://forum.kirupa.com/u/Tirumalesh_Chinna)
#### Post date: [December 23, 2017, 8:03pm UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/19 "2017-12-23T20:03:12Z")

</div>

Why there is a “#” in the urls,

Ex: [http://localhost:3000/#/contact](http://localhost:3000/#/contact)

how to remove that?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 26, 2017, 3:22am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/20 "2017-12-26T03:22:42Z")

</div>

That is because you (and the tutorial) are using `HashRouter`. The `BrowserRouter` component should avoid that: [https://reacttraining.com/react-router/web/api/BrowserRouter](https://reacttraining.com/react-router/web/api/BrowserRouter) 🙂

---

<div class="post-metadata">

### Author: ![Fictionfree](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Fictionfree](https://forum.kirupa.com/u/Fictionfree)
#### Post date: [December 27, 2017, 8:16am UTC](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201/21 "2017-12-27T08:16:25Z")

</div>

I first made a simple nav with home ,contact ,about and with components having about, home,etc each having an index.jsx. Now I want to add a few sub sections like vision, acknowledgement ,etc in about section like a drop-down. I made different folders for them and linked them up but since I already had an about page , when I click on the about link in navbar, the drop down opens up as well the about page, which I don’t want anymore. What should I do?

[Next page](https://forum.kirupa.com/t/creating-a-single-page-app-in-react-using-react-router/636201.md?page=2)
