Skip to Content

Get Started with UI5 Web Components for React

Beginner
15 min.
Start building a React web application leveraging UI5 Web Components for React.
You will learn
  • How to create a new React application
  • How to connect the application with UI5 Web Components for React
Lukas742Lukas HarbarthAugust 19, 2022
Created by
Lukas742
December 5, 2019

Prerequisites

React is a great front-end development tool for building single-page applications (SPA). UI5 Web Components for React provides a SAP Fiori-compliant React implementation by leveraging the UI5 web components.

SAP Fiori provides a consistent and holistic user experience for SAP software. By creating visually pleasing designs with a strong focus on ease of use, the experience is intuitive and simple, across all devices.

This first tutorial will start by creating a React application that is able to consume UI5 Web Components for React.

  • Step 1

    Navigate to a folder where you want to create your Web App and open a terminal there. Then use the following command:

    Shell
    Copy
    npx create-react-app my-app --template @ui5/cra-template-webcomponents-react
    cd my-app
    

    This command leverages create-react-app to create a React Application with all necessary dependencies for UI5 Web Components for React.

  • Step 2
    1. Open the current directory with an editor of your choice (e.g. Visual Studio Code).

    2. Inside of your project folder, navigate to src. There, create a new file and name it MyApp.jsx.

    3. Now, add the following lines of code to MyApp.jsx.

      JavaScript / JSX
      Copy
      import React from "react";
      
      export function MyApp() {
        return <div>My root component</div>;
      }
      

    Structure of a React component

    This is a very simple component, but it already shows you the basic structure of all components. The file starts with the import statements in the first few lines. Then, the component will be defined as a function (or as a class). This function starts the definitions of the props and the logic, we’ll add them in a later tutorial, and ends by returning JSX or HTML components in a return statement.

    With this you created your first React component. To actually render the component you will have to add it to your src/App.js.

  • Step 3
    1. In App.js remove everything inside of the <ThemeProvider> (or React.Fragment <></>). You can also delete the className property and the import of the logo and "./App.css" as they are not needed anymore for this tutorial.

    If the ThemeProvider is not present in this file, please look in the index.js file to see if the component is there. If it is, you do not need to use it here as well, otherwise please continue as described.

    1. Import your created component.
      JavaScript / JSX
      Copy
      import { MyApp } from "./MyApp";
      
    2. Add the component to the return value of App().
      JavaScript / JSX
      Copy
      function App() {
        return (
          <ThemeProvider>
            <MyApp />
          </ThemeProvider>
        );
      }
      

    Your App.js file should now look like this:

    JavaScript / JSX
    Copy
    import React from "react";
    import { MyApp } from "./MyApp";
    import { ThemeProvider } from "@ui5/webcomponents-react";
    
    function App() {
      return (
        <ThemeProvider>
          <MyApp />
        </ThemeProvider>
      );
    }
    
    export default App;
    
  • Step 4

    Now you can start the app in development mode. Execute the following command from the root directory of the project.

    Shell
    Copy
    npm start
    

    Open http://localhost:3000 to view it in the browser.

    Root Component

    The page will automatically reload if you make changes to the code. You will see the build errors and lint warnings in the console.

    TIP: You can put a browser window next to the editor in your screen to see the changes live in action.

    split

     

    How can you install the UI5 Web Components for React and add it to your package.json?

Back to top