Scaffolding a React app with Vite

Installing Vite and using it to build a basic ReactJS app with functional components, and CSS and image files.

Learning Goals

At the end of this Tutorial, you will be able to:

  • Scaffold a ReactJS app with Vite and be familiar with its folder and file structure.
  • Customise the template files according to your requirements.
  • Build a basic app with functional components, and imported CSS and image files.

About Vite

Vite is a modern build tool that provides a streamlined workflow for building web applications, especially those using React. It has replaced the older utility called create-react-app.

Scaffolding your first app with Vite

Follow these steps to build a basic ReactJS app with Vite:

  1. In a Command Prompt window or inside a VS Code Terminal, navigate to where you want Vite to create a folder for your app. For example:
    C:\> users\react\apps
  2. Type the following command that includes the name you want to call your new app. In this example, the app is called app-react-basic.
    npm create vite@latest app-react-basic -- --template react
  3. If you do not already have Vite installed, you will be prompted to install it. screenshot
  4. When Vite completes the build process, you will see the message below. screenshot
  5. Follow these instructions to continue building your app, and then running it. screenshot

When you launch your new app in a browser, it should look as shown below.

screenshot

ReactJS folders and files

Vite will create the following hierarchy of folders and files.

ReactJS sample screen

The more important items are as listed below.

index.html

The web page that is served when your app is run. It has a <div id="root"></div> container into which main.jsx injects the top-level or parent <App /> component.

The /src folder

This contains your app's components. All of the code you write will be located here.

main.jsx

The main JavaScript file where your React app is initialized.

App.jsx

The root React component.

vite.config.js

The configuration file for Vite.

Your App.jsx file may contain all the code for your ReactJS app. But more commonly, it acts as a parent component that imports other, single-purpose 'child' components.

As a test, add some new text to App.jsx, save the file, and verify that your app updates live in your web browser.

ReactJS sample screen

Customising your app

Next, you will customise the 'boilerplate' content provided by Vite to build a very basic React app.

  1. Download the following stylesheet and save it to your app's /src folder.   Petite.css
  2. Open the index.html file in your main app folder and replace the <title> tag as shown below. screenshot
  3. In your /src folder, open the main.jsx file and update it as follows. screenshot
  4. Also in your /src folder, open App.jsx and replace all its content with the content below.
  5. import './Petite.css'
    
    function App() {
      return (
          <>
             <h1>Hello, World!</h1>
             <p>Paragraph of text.</p>
          </>
      );
    }	
    
    export default App;

Your web page should now look as shown below.

Sample React screen

In this basic app, you have added code directly to the App.jsx file.

Adding child components

Next, you will add three components to your app. When finished, your App.jsx file will be the parent component and three others will all be child components.

  1. In the /src folder, create a Header.jsx file with the following content.
    function Header() {
       return (
          <header>
              <h1>Corporate strategy</h1>
          </header>  
       )
    }
    	
    export default Header;
  2. Also in the /src folder, create a MainContent.jsx file with the following content.
    function MainContent() { 
       return (
          <main>
             <section>
    	     <p>We leverage agile frameworks to provide a robust synopsis for high-level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition.</p>
    	  </section>
          </main>  
        )
    }
     
    export default MainContent;
  3. And finally in the /src folder, create a Footer.jsx file with the following content.
    function Footer() {
       return (
    	<footer>
    	   <p>ABC Corporation. Digital Consultants</p>
    	</footer>  
       )
    }
    	
    export default Footer;
  4. Now, update your App.jsx file so that it acts as the parent to your three new components. See below.
    import Header from './Header';
    import MainContent from './MainContent';
    import Footer from './Footer';
    import './Petite.css'
    	
    function App() {
       return (
         <>
             <Header />
             <MainContent />
             <Footer />
         </>
       );
    }
    	
    export default App;

Your web page should now look similar to the following.

Sample React screen

The parent-child relationships between the various files in your ReactJS app are illustrated below.

About CSS files

In non-ReactJS websites, you would typically add a CSS file by including a link to it in the <head>. For example.

React sample screen

For a ReactJS app, however it is better to place your CSS file(s) and any images in your /src folder (or a sub-folder of it) and import them with JavaScript.

Adding an image file

As with CSS files, it is best to add them from the /src folder with import statements.

The only exceptions might be for generic site-wide images which would be tedious to manually import multiple times into different component files. Or when the React app is only part of a larger website.

You can import an image into your sample app as follows.

  1. Download the following file to your /src folder.   corporate.jpg
  2. In your MainContent.jsx child component, add the following import statement at the top of the file.
    import corporateImg from './corporate.jpg';
  3. Now you can reference the image by its variable name. See below.
    function MainContent() { 
       return (
          <main>
             <section>
                <img src={corporateImg} alt="Corporate" />
                <p>We leverage agile frameworks to provide a robust synopsis for high-level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition.</p>  
             </section>
          </main>
       )
    }
    
    export default MainContent;

Your web page should now look as shown below.

Sample React screen