Create a Single Page Application with React and React Router
Tags: code
Published: 2021-03-19 17:25:26

---
title: Create a Single Page Application with React and React Router
published: true
date: 2021-02-16 19:02:17 UTC
tags: Code
canonical_url: https://www.split.io/blog/react-router-feature-flags/
---

JavaScript devs have tons of options today, and React holds its place as a leading library because it’s so easy to use and tool-rich. Creating and building a React app has never been easier, especially with React Router. React Router conditionally renders specific components to display depending on the route that you use in the URL. 

With React Router, you can create multiple routes to determine which component should render based on the link that was clicked. If you’re building a single-page application, you should use React Router to render specific components that act like separate pages – making it look like your website is made up of more than one page. Let’s go through an example.

## Prerequisites 

First, let’s make sure you have Node installed. If not, follow [these instructions](https://howtonode.org/how-to-install-nodejs).

You’ll also want to sign up for a [free developer account with Split](https://www.split.io/signup/) if you haven’t already. Split is a feature flagging and experimentation platform that we will be using in our app. Later on, you’ll incorporate our JavaScript SDK with your app.

## Create Your React App

We will use `create-react-app` to create a travel blog website and use React Router. [Here](https://github.com/talianassi921/react-router-app) is the full repo to follow along.

`npx create-react-app react-router-blog-example`

`cd react-router-blog-example`

`npm start`

You should automatically be routed to `localhost:3000` unless you have something else running on that port. You should see the default React app template:

![](https://lh6.googleusercontent.com/nP14Eo37_Q5GZ2Hcanrk5ES3xUAMXsjhTtitACwMCrRXmu3rP9iAk4Q9SaXms_TbEwImCOxMvZKdOCWHJvlGbCQpskBwxLuM1Iu2Ui0phLo4rlbwTgirgKzx8UitqXql4hVncWMv)

## Create Your Paths and Routes with React Router

Now, let’s set up our `App.js `file. First, we are going to create a list of links: Home, About, and Users. 

```

` <ul>
           <li>
             <Link to="/">Home</Link>
           </li>
           <li>
             <Link to="/about">About</Link>
           </li>
           <li>
             <Link to="/cuisine">Cuisine</Link>
           </li>
         </ul>
`
<small id="shcb-language-1"><span>Code language:</span> <span>Django</span> <span>(</span><span>django</span><span>)</span></small>
```

Next, we need to add a separate <Route> for each page. Think of this as different URLs for each link. Although we are making a single page application, we will still have different links for different parts of the app. Let’s add a path that will correspond to each of our links and wrap them all in a <Switch>. A <Switch> looks through its children’s Routes and renders the first one that matches the current URL.

```

`<Switch>
         <Route path="/about">
           <About />
         </Route>
         <Route path="/cuisine">
           <Cuisine />
         </Route>
         <Route path="/">
           <Home />
         </Route>
</Switch>
`
<small id="shcb-language-2"><span>Code language:</span> <span>Django</span> <span>(</span><span>django</span><span>)</span></small>
```

## Insert Your Components

Notice in each path, we have the corresponding component. Then, we need to add content to our components. In this example, I am using the [rebass component library](https://rebassjs.org/), but feel free to use any component library you like. We are going to create three different cards for each of the links: HomeCard, AboutCard, and CuisineCard, which will each be the main component of the different pages. First, I import the component from rebass, choose an image to be in the card, and then add it to my App.js file.

```

`class HomeCard extends React.Component {
 render() {
   return (
     <Box width={500}>
       <Card
         sx={{
           p: 1,
           borderRadius: 2,
           boxShadow: '0 0 16px rgba(0, 0, 0, .25)',
         }}
       >
         <Image src={image} />
         <Box px={2}>
           <Heading as="h3">Talia's Travels</Heading>
           <Text fontSize={0}>Bon Voyage!</Text>
         </Box>
       </Card>
     </Box>
   );
 }
}

export default HomeCard;
`
<small id="shcb-language-3"><span>Code language:</span> <span>JavaScript</span> <span>(</span><span>javascript</span><span>)</span></small>
```

Your app should look like this:

![](https://lh6.googleusercontent.com/FL6ezE2E2fI1JA6LFTkMCzwIC5mlZViV1EDNBjzH8gCzSPlCyPzK32ZSpeQxeoAEGORPCFmHkMjQGk8SxKc6QAGLD3Bh1FukmnBFTqBigauRHH_h_TCdWxF422HW3_5hEbQaKc01)

We are going to duplicate the HomeCard to create the AboutCard and the CuisineCard. Go ahead and choose an image for each.

![](https://lh3.googleusercontent.com/scXR_vKw1YePpsYO9lCh03LqT3sZQ6bqgwjrBMJcv_Uo0n_nizGmrKmhH-kJpYFPMOx-nPVDz-jUoBhK9QGol0tWezX7X1SSDmadiUaC5l7tClSQVsavDlS3B4HrjLuvHj1q7_j1)

![](https://lh6.googleusercontent.com/wdqi4Mofr_Fw91b-k8IIbAFNNeqRhRoYFqvd0SpPmplzHWTjweezcmFu-ai1pzOMrdk_kOE-ye-z2qPUCCt-ck51T25qJqvpAqUlHeqztMeZ64p0dpuoGdachQ6pyEtncjiqSbyq)

You can add more text if you like, or more cards. 

## Add Feature Flags to Your React App

Let’s say I only wanted certain users to have access to my travel blog, because I am going to add personal pictures with my friends and family. Using feature flags is a great way to target specific users and make sure only the intended userbase sees your app or feature. To download Split, sign up for a [free developer account](https://www.split.io/signup/). 

Then, find the Create a Split button on the left panel of the homepage. Enter a name for your flag that you will later input into your code. Then, select a treatment type. The treatment differentiates the people who see your app and the people who don’t. In this case, select user because you want to segment a group of users to be able to see your personal travel photos.

Next, it’s time to add your targeting rules. In the `Set Targeting Rules` section, enter the email addresses of the users who should see the app, while keeping the default off.

You’ll then want to enter the API key from the UI and insert that into your sdkConfig in your component.

## React Router to the Rescue

[React Router](https://reactrouter.com/) is a great way to build single-page applications because you prevent a page refresh every time a link is clicked. With client-side rendering, the page doesn’t refresh as you navigate through the different links. React Router is easy to understand, and simple to implement!

## Learn More About React and Feature Flags

Feature flags give you the ability to roll out your app in a controlled fashion to only the selected population. Here at Split, we don’t write code without them!

- [Set Up Feature Flags with React in 10 Minutes](https://www.split.io/blog/react-feature-flags-10-minutes/)
- [Set Up Feature Flags with React Hooks](https://www.split.io/blog/react-hooks-feature-flags/)
- [Testing a Feature Flagged Change](https://www.split.io/blog/testing-feature-flagged-change/)

To stay up to date with all of our content, be sure to [follow us on Twitter](https://twitter.com/SplitSoftware) and [subscribe to our Youtube Channel](https://www.youtube.com/channel/UCrkFMZZY8cjmZyKzOVV245Q/videos)!