{"id":5220,"date":"2020-04-23T13:13:00","date_gmt":"2020-04-23T13:13:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=5220"},"modified":"2020-08-22T14:57:42","modified_gmt":"2020-08-22T14:57:42","slug":"how-to-create-a-single-page-app-using-react","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/how-to-create-a-single-page-app-using-react\/","title":{"rendered":"How to create a single-page app using React?"},"content":{"rendered":"\n<p>Single-page apps are websites that have pages that load inline within the same page. Read on to find how you can create a single page application using React.<\/p>\n\n\n\n<h2><strong>What is a single page app?<\/strong><\/h2>\n\n\n\n<p>A single page application (SPA) is essentially a webpage that interacts with the web browser dynamically by rewriting the current web page with the data obtained from the webserver. Hence, in a single page application, the webpage does not reload the page during its runtime and instead works within a browser.&nbsp;<\/p>\n\n\n\n<h2><strong>Single page app vs. multi-page app<\/strong><\/h2>\n\n\n\n<h3>Single-page App<\/h3>\n\n\n\n<p><strong>Reloading<\/strong>: Single-page applications work inside a browser and do not require page reloading during webpage executing.&nbsp;<\/p>\n\n\n\n<p><strong>UI\/UX<\/strong>: Offers outstanding user experience by imitating a natural environment with the browser by eliminating wait time and page reloads. It consists of a single web page that loads all content using JavaScript. It requests the markup and data independently and renders pages straight to the browser.&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>Examples<\/strong>: Gmail, Google Maps, Facebook, GitHub.<\/p>\n\n\n\n<h3>Multi-page App<\/h3>\n\n\n\n<p><strong>Reloading<\/strong>: Multi-page applications work in the traditional way where every change, like displaying the data or submitting data back to the server, renders new pages from the server.<\/p>\n\n\n\n<p><strong>UI\/UX<\/strong>: Multi-page applications are larger applications with huge chunks of content, so the user experience is limited compared to single-page applications.&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>Examples<\/strong>: eBay and Amazon&nbsp;<\/p>\n\n\n\n<h2><strong>Why choose a single-page app?<\/strong><\/h2>\n\n\n\n<p>The benefits of choosing single-page applications (SPA) are:<\/p>\n\n\n\n<ul><li>SPA is quicker since all the webpage resources are loaded only once throughout the application, and data is the only resource that is transmitted.<\/li><li>SPA caches local storage effectively as it sends one request, stores all the data, and uses it even when offline.<\/li><li>SPA simplifies and streamlines development activities as it eliminates the need to write code to render pages on the server.<\/li><li>SPA can be debugged with ease with Chrome as it is possible to investigate page elements and monitor network operations.<\/li><\/ul>\n\n\n\n<h2><strong>When not to use single-page app?<\/strong><\/h2>\n\n\n\n<p>While SPA does have its advantages, there are certain cases when it is not suitable to use it:<\/p>\n\n\n\n<ul><li>SEO: It is difficult and tricky to optimize SPA for SEO since its content is loaded by AJAX (Asynchronous JavaScript and XML). Hence SPAs are not suitable for cases where SEO is critical for business success.&nbsp;<\/li><li>Javascript: It requires users to enable Javascript for proper application and action loading. So it is not suitable for instances where JavaScript might be disabled on the user side.&nbsp;<\/li><li>Security: SPA is also less secure in comparison to MPA, making it unsuitable for highly sensitive applications. SPA has a cross-site scripting (XSS) and allows attackers to inject client-side scripts into the web application.<\/li><li>Slow: While the user experience of SPAs on runtime is fast, it is slower to download and can also be slowed down if there are memory leaks in JavaScript. It is hence not suitable for very large applications with a lot of data.<\/li><\/ul>\n\n\n\n<h2><strong>How to build a single-page app using React?<\/strong><\/h2>\n\n\n\n<p>To build a single page app using React, follow the steps mentioned below:<\/p>\n\n\n\n<p>1. Create a react app in your desired location using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npx create-react-app app-name<\/em><\/pre>\n\n\n\n<p>A directory called&nbsp;<em>app-name<\/em>&nbsp;is created with the following default files:<\/p>\n\n\n\n<p>app-name<\/p>\n\n\n\n<p>\u251c\u2500\u2500 README.md<\/p>\n\n\n\n<p>\u251c\u2500\u2500 node_modules<\/p>\n\n\n\n<p>\u251c\u2500\u2500 package.json<\/p>\n\n\n\n<p>\u251c\u2500\u2500 .gitignore<\/p>\n\n\n\n<p>\u251c\u2500\u2500 public<\/p>\n\n\n\n<p>\u2502 &nbsp; \u251c\u2500\u2500 favicon.ico<\/p>\n\n\n\n<p>\u2502 &nbsp; \u251c\u2500\u2500 index.html<\/p>\n\n\n\n<p>\u2502 &nbsp; \u251c\u2500\u2500 logo192.png<\/p>\n\n\n\n<p>\u2502 &nbsp; \u251c\u2500\u2500 logo512.png<\/p>\n\n\n\n<p>\u2502 &nbsp; \u251c\u2500\u2500 manifest.json<\/p>\n\n\n\n<p>\u2502 &nbsp; \u2514\u2500\u2500 robots.txt<\/p>\n\n\n\n<p>\u2514\u2500\u2500 src<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 App.css<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 App.js<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 App.test.js<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 index.css<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 index.js<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500 logo.svg<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u2514\u2500\u2500 serviceWorker.js<\/p>\n\n\n\n<p>2. Install&nbsp;<em>react-router-dom<\/em>&nbsp;for routing requests by executing the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>npm install react-router-dom&nbsp;<\/em><\/pre>\n\n\n\n<p>3. Wrap the&nbsp;<em>App<\/em>&nbsp;component.<\/p>\n\n\n\n<p>There are two types of React routers,&nbsp;<em>BrowserRouter<\/em>&nbsp;(makes URLs like example.com\/about) and&nbsp;<em>HashRouter<\/em>&nbsp;(makes URLs like example.com\/#\/about). We re using&nbsp;<em>BrowserRouter<\/em>&nbsp;in this example and use it to wrap the&nbsp;<em>App<\/em>&nbsp;component.<\/p>\n\n\n\n<p>Your&nbsp;<em>src\/index.js<\/em>&nbsp;file should include the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from \u2018react\u2019\nimport { render } from \u2018react-dom\u2019 \nimport { BrowserRouter } from \u2018react-router-dom\u2019 \nimport App from \u2018.\/App\u2019 \nrender( \n \u00a0\u00a0&lt;BrowserRouter> \n \u00a0\u00a0\u00a0\u00a0&lt;App \/> \n \u00a0\u00a0&lt;\/BrowserRouter>, \n \u00a0\u00a0document.querySelector(\u2018#root\u2019) \n) <\/pre>\n\n\n\n<p>4. Create a file named&nbsp;<em>src\/pages\/HomePage.js<\/em>&nbsp;with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from \u201creact\u201d;\nexport default function HomePage() { \n return ( \n  &lt;> \n  &lt;h1>Hey from HomePage&lt;\/h1> \n  &lt;p>This is your awesome HomePage subtitle&lt;\/p> \n  &lt;\/> \n ); \n}<\/pre>\n\n\n\n<p>5. Create a file named<em>&nbsp;src\/pages\/UserPage.js<\/em>&nbsp;with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from \u201creact\u201d;\nimport { useParams } from \u201creact-router-dom\u201d; \nexport default function UserPage() { \n let { id } = useParams(); \n return ( \n  &lt;> \n  &lt;h1>Hello there user {id}&lt;\/h1> \n  &lt;p>This is your awesome User Profile page&lt;\/p> \n  &lt;\/> \n ); \n}<\/pre>\n\n\n\n<p>6. Decide and incorporate the routers that you want to use using&nbsp;<em>Switch<\/em>&nbsp;and&nbsp;<em>Route<\/em>.&nbsp;<em>Switch<\/em>&nbsp;groups all routes together and ensures that they take the precedence from top-to-bottom.&nbsp;<em>Route,<\/em>&nbsp;on the other hand, defines individual routes.<\/p>\n\n\n\n<p>Your&nbsp;<em>App.js&nbsp;<\/em>file should include the decided routes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from \u2018react\u2019 \nimport { Route, Switch } from \u2018react-router-dom\u2019 <\/pre>\n\n\n\n<p>\/\/ We will create these two pages in a moment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import HomePage from \u2018.\/pages\/HomePage\u2019\nimport UserPage from \u2018.\/pages\/UserPage\u2019 \nexport default function App() { \n \u00a0\u00a0return ( \n \u00a0\u00a0\u00a0\u00a0&lt;Switch> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Route exact path=\u201d\/\u201d component={HomePage} \/> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Route path=\u201d\/:id\u201d component={UserPage} \/> \n \u00a0\u00a0\u00a0\u00a0&lt;\/Switch> \n \u00a0\u00a0) \n}<\/pre>\n\n\n\n<p>The above code matches the root route (\/) to&nbsp;<em>HomePage<\/em>&nbsp;and matches other pages dynamically to&nbsp;<em>UserPage<\/em>.&nbsp;<\/p>\n\n\n\n<p>7. Link to a page within the SPA using Link.&nbsp;<\/p>\n\n\n\n<p>In the&nbsp;<em>src\/pages\/HomePage.js<\/em>&nbsp;file, include the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from \u2018react\u2019\nimport { Link } from \u2018react-router-dom\u2019\nexport default function HomePage() { \n \u00a0\u00a0return ( \n \u00a0\u00a0\u00a0\u00a0&lt;div className=\u201dcontainer\u201d> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;h1>Home &lt;\/h1> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;p> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Link to=\u201d\/your desired link\u201d>Your desired link.&lt;\/Link> \n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/p> \n \u00a0\u00a0\u00a0\u00a0&lt;\/div> \n \u00a0\u00a0) \n}<\/pre>\n\n\n\n<p>You can now run the code and view the development server available at&nbsp;<a href=\"http:\/\/localhost:3000\/\">http:\/\/localhost:3000<\/a>.&nbsp;<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>That\u2019s all for now, folks! We hope this <strong>article <\/strong>will help you start your project using <strong>React<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>If you have any questions or suggestions, please feel free to write in the comments section below! And don&#8217;t forget to like and share this article on social media. Enjoy!<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Single-page apps are websites that have pages that load inline within the same page. Read on to find how you can create a single page application using React. What is a single page app? A single page application (SPA) is essentially a webpage that interacts with the web browser dynamically by rewriting the current web [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5221,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,77,62],"tags":[25,146,73,133,110],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/how-to-create-a-single-page-app-using-react.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1mc","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5220"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=5220"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5220\/revisions"}],"predecessor-version":[{"id":5222,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5220\/revisions\/5222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/5221"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=5220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=5220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=5220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}