Creating A Single Page Application In SPFx

We all know that Microsoft is trying everything it can to modernize the SharePoint experience for its users. The main reason behind this is to give consumers the freedom to customize the SharePoint they use.
Another advantage of opting for a modern look over the classic look is that it is responsive in nature. When you create a site or a page with the modern experience then you need not worry about how it’ll look on different devices. Microsoft handles it for you.
With this being said, let’s move on with our SharePoint framework web part.

SharePoint Framework

Again, this will leave us with a question of why SPFx. This is because it lets you create custom apps for your SharePoint and it is easier to develop and deploy the application packages.

Getting Started

You can refer to the “Getting Started” section of my previous blog here Or you can also refer the official Microsoft Docs on building your first client-side web part.
After creating the SPFx project, open your code editor and navigate to your project directory.
You can see that yeoman has beautifully created your project structure.
Creating A Single Page Application In SPFx
Now, we’ll start building our SPA (Single Page Application) with SharePoint Framework.

What is a Single Page Application?

An SPA is a web application which will dynamically update your changes in the DOM without reloading the page itself.
When you have one or more components and you want to switch between these components, but you don’t want the page to reload every time, then the ideal approach for this is SPA.

Integrating SPA with SPFx

Coming back to our code, one of the important modules in Single Page Applications is Router.
Router takes care of the path to redirect a particular component.
  • Open the command prompt and go to your project directory.
  • Now, install

    “npm i react-router-dom”
Creating A Single Page Application In SPFx

Building Our Components

Let’s first design the layout of our application.
Creating A Single Page Application In SPFx
You can design the Application layout as you wish. Now we’ll start building these components first.
Create a separate folder for each of your components.
Creating A Single Page Application In SPFx 

The Routing Part

Move to <your-application>.tsx which will act as the routing component. (In my case it is “SinglePageApp.tsx”)
Import the modules required for Routing,
  1. import { BrowserRouter as Router, Route, Link, Redirect, Switch, HashRouter, PropsRoute } from 'react-router-dom';  
Import all the Components that the single page application requires.
  1. import HomePage from './DashBoard/index';  
  2. import AppPage from './Apps/App_Page';  
  3. import Pages from './Pages/Pages';  
  4. import Navigation from '../components/SideNav/Navigation';  
Modify the render function to adapt to the routing.
  1. export default class SinglePageApp extends React.Component<ISinglePageAppProps, {}> {  
  2.   public render(): React.ReactElement<ISinglePageAppProps> {  
  3.     return (  
  4.       <HashRouter >  
  5.         <Stack horizontal gap={15}>   
  6.           <Navigation />  
  7.           <StackItem grow={2}>  
  8.             <Switch>  
  9.               <Route path='/' exact={true} component={() => <HomePage  {...this.props} />} />  
  10.               <Route path='/Apps' component={() => <AppPage  {...this.props} />} />  
  11.               <Route path='/Pages' component={() => <Pages  {...this.props} />} />  
  12.             </Switch>  
  13.           </StackItem>  
  14.         </Stack>  
  15.       </HashRouter>  
  16.     );  
  17.   }  
  18. }  
You can see that I’ve used,
  1. <Route path='/' exact={true} component={() =><HomePage  {...this.props} />} />    
This piece of code takes care of the actual routing process.
  • Path
    The URL path where you want to render your component.

  • Exact
    It is used to tell the application to strictly render the component only if the path is the same as mentioned.

  • Component
    Here you can define which component to render and where to render.
The whole SinglePageApp.tsx will look like this.
  1. import * as React from 'react';  
  2. import { ISinglePageAppProps } from './ISinglePageAppProps';  
  3. import { Stack, StackItem } from 'office-ui-fabric-react';  
  4. import HomePage from './DashBoard/index';  
  5. import AppPage from './Apps/App_Page';  
  6. import Pages from './Pages/Pages';  
  7. import Navigation from '../components/SideNav/Navigation';  
  8. import { BrowserRouter as Router, Route, Link, Redirect, Switch, HashRouter, PropsRoute } from 'react-router-dom';  
  12. export default class SinglePageApp extends React.Component<ISinglePageAppProps, {}> {  
  13.   public render(): React.ReactElement<ISinglePageAppProps> {  
  14.     return (  
  15.       <HashRouter >  
  16.         <Stack horizontal gap={15}>   
  17.           <Navigation />  
  18.           <StackItem grow={2}>  
  19.             <Switch>  
  20.               <Route path='/' exact={true} component={() => <HomePage  {...this.props} />} />  
  21.               <Route path='/Apps' component={() => <AppPage  {...this.props} />} />  
  22.               <Route path='/Pages' component={() => <Pages  {...this.props} />} />  
  23.             </Switch>  
  24.           </StackItem>  
  25.         </Stack>  
  26.       </HashRouter>  
  27.     );  
  28.   }  
  29. }  
As a final step, we’ll have to modify our Navigation.tsx file so that on clicking, we’ll get redirected to that component.
  1. links: [  
  2.   {  
  3.     iconClassName: styles.button,  
  4.     name: 'DashBoard',  
  5.     url: '#/',  
  6.     key: 'key2',  
  7.     isExpanded: true,  
  9.   },  
  10.   {  
  11.     name: 'Apps',  
  12.     url: '#/Apps',  
  13.     key: 'key3',  
  14.     isExpanded: true,  
  15.   },  
  16.   {  
  17.     name: 'Pages',  
  18.     url: '#/Pages',  
  19.     key: 'key4',  
  20.   }  
  21. ]  
Run “gulp serve” and go to your localhost.
Creating A Single Page Application In SPFx
Click different components to see the routing changes.
Creating A Single Page Application In SPFx
Wow! We have created our Single Page Application successfully. Follow me to catch up with articles on SPFx and React.