User interface development with React
Summarize
Summary of User interface development with React
ServiceNow enables customers to develop user interfaces (UIs) using the React library, an industry-standard framework for building reusable UI components with JavaScript or TypeScript. React applications are created and managed via the ServiceNow IDE or ServiceNow SDK. These tools provide React templates for rapid setup, and the UI pages are defined using the ServiceNow Fluent UI Page API, which links to an HTML entry point (index.html) and manages the static assets upon application installation.
Show less
UI Development Process
- Create an application using a React template, which includes necessary files and directory structure for React development.
- Add client-side assets in the
src/clientdirectory, including the HTML entry point (index.html), scripts, and stylesheets. - Define a UI page using the ServiceNow Fluent UI Page API in a
.now.tsfile, importing the HTML file and referencing it in the UI page object. - Build the application, which triggers a Rollup bundling process for dependencies and packages client assets into the
dist/staticdirectory. - Install the application to upload static assets to appropriate ServiceNow tables that handle UX assets, images, and themes.
Important Considerations
- React UI development is currently experimental due to limitations in supported content types and instance serving methods.
- Modifications to UI page HTML should only be done in source code; edits directly on the instance are not synchronized back and may cause issues.
- Certain content types like audio, video, and WASM are unsupported, and attachment file sizes are limited by system property settings.
- Technical constraints include restrictions on preload links, relative stylesheet imports, CSS modules, server-side rendering, and routing methods (only hash routing supported).
- Next Experience Components can be integrated via the React Wrapper Component Library available through npm or ServiceNow build tools.
Practical Usage
Developers define UI pages in Fluent code within .now.ts files, importing HTML and linking to React entry points such as main.jsx, which renders the React component tree into the DOM. After building and installing, the UI page is accessible via a specified endpoint on the ServiceNow instance, providing a fully functional React-based interface (for example, an Incident Response Manager UI).
Benefits for ServiceNow Customers
- Leverage modern React development practices within the ServiceNow platform for enhanced UI flexibility and reusability.
- Use provided templates and Fluent API for streamlined UI page creation and asset management.
- Integrate third-party React components to enrich user experience through the React Wrapper Component Library.
- Deploy full-stack applications with bundled client assets managed by ServiceNow’s build and installation processes.
Develop a user interface (UI) with the React library to build a full-stack application in source code.
Overview of UI development with React
React is an industry-standard web framework for building UI components that you use to develop and render custom user interfaces. A React component is a self-contained, reusable JavaScript function, such as a button, and supports JavaScript (JS/JSX) and TypeScript (TS/TSX) syntax. For general information about React, see the React documentation on the react.dev website.
With the ServiceNow IDE or ServiceNow SDK, you can use React in an application to create a UI page in ServiceNow Fluent code. The ServiceNow Fluent UI Page API refers to an HTML entry point (index.html) that loads the page at the endpoint provided. After building and installing the application on an instance, the static assets are stored in the appropriate tables. For an example of a React application in source code, see the ServiceNow SDK examples GitHub repository. To get started using React, select one of the React templates when creating an application with the ServiceNow IDE or ServiceNow SDK.
UI development process
The following list is a high-level overview of the process to develop a React application with the ServiceNow IDE or ServiceNow SDK:
- Create an application and select one of the React templates.
The application includes the files and directories required for React development.
- In the src/client directory, add client assets for a user interface, such as an HTML entry point (index.html), client scripts, and style sheets. The index.html file must
contain the
<script>tag as the JavaScript entry point. - Define a UI page [sys_ui_page] in code with the ServiceNow Fluent UI Page API. In the now.ts file that contains the UI page definition, you import the HTML and refer to it from the html property of the UiPage object.
- Build the application to execute a pre-build Rollup script that bundles dependencies and packages the client assets in the src/client directory before executing the
rest of the build process. The standard Rollup build process and plugins are used as the default JavaScript bundler.
Static assets are output to the dist/static directory.
- Install the application to add the static assets to the application files [sys_metadata] in the appropriate tables: UX Asset [sys_ux_lib_asset], Images [db_image], and UX Theme Asset [sys_ux_theme_asset] tables. These tables support
adding files as attachments.Important:UI development with React is an experimental feature due to how content is served from an instance and the content types and tables that are currently supported.
Limitations
- The UI page HTML should be modified only in source code. Changes to the HTML of a UI page [sys_ui_page] on an instance aren't synchronized into source code and are likely to result in unintended behavior.
- Audio, video, and WASM content types aren't supported.
- The maximum file size of uploaded attachments is limited by the size configured with com.glide.attachment.max_size system property. For more information, see Maximum allowed attachment size.
- Output paths must be deterministic.
- Pre-loading content linked from HTML isn't supported (
rel="preload"). - Relative style sheets linked from HTML aren't supported (
rel="stylesheet"). Import style sheets into code instead (@import 'path/to/style-sheet'). - Relative imports in CSS aren't supported.
- CSS modules aren't supported.
- Only hash routing is supported by UI pages.
- Server-side rendering and React server components aren't supported.
UI page development with React
In a .now.ts file in the src/fluent directory, a UI page is defined in ServiceNow Fluent code. The HTML of the page is imported from the index.html file in the src/client directory.
//incident-manager.now.ts
import '@servicenow/sdk/global'
import { UiPage } from '@servicenow/sdk/core'
import incidentPage from '../../client/index.html'
UiPage({
$id: Now.ID['incident-manager-page'],
endpoint: '<scope>_incident_manager.do',
description: 'Incident Response Manager UI Page',
category: 'general',
html: incidentPage,
direct: true,
})
In the index.html file, the <script> tag refers to the main.jsx file in the src/client directory.
//index.html
<html>
<head>
<title>Incident Response Manager</title>
<!-- Initialize globals and Include ServiceNow's required scripts -->
<sdk:now-ux-globals></sdk:now-ux-globals>
<!-- Include your React entry point -->
<script src="./main.jsx" type="module"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
//main.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './app'
const rootElement = document.getElementById('root')
if (rootElement) {
ReactDOM.createRoot(rootElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
}After building and installing the application, you can open the page from the endpoint provided in the UiPage object (https://<instance>/<scope>_incident_manager.do), which displays an interface for creating and managing incidents.