@quixomatic/ui-renderer-react-simple
v1.1.1
Published
React 18 renderer for ServiceNow UI Framework with automatic setup and babel plugin patching
Maintainers
Readme
@quixomatic/ui-renderer-react-simple
A React 18 renderer for ServiceNow UI Framework that provides seamless integration with Next Experience components.
Features
- React 18 Support: Full compatibility with React 18 hooks and concurrent features
- Automatic Setup: One-command installation that handles all configuration
- Babel Plugin Patching: Automatically patches ServiceNow's babel plugin for React support
- Drop-in Replacement: Compatible with existing ServiceNow React patterns
- Error Boundaries: Built-in error handling for React components
- VNode Filtering: Prevents Snabbdom VNode contamination
- No Manual Configuration: Zero manual babel plugin modifications required
Quick Start
# Install the package
npm install @quixomatic/ui-renderer-react-simple react@18 react-dom@18
# Run the setup script (creates fake package + patches babel)
npx setup-servicenow-react
# Complete the setup
npm installCLI Commands
npx setup-servicenow-react- Complete setup (fake package + babel patch)npx patch-servicenow-babel- Just patch the babel pluginnpx restore-servicenow-babel- Restore original babel plugin
Usage
After setup, you can use React components exactly like you would with the original @quixomatic/ui-renderer-react:
// my-component/index.js
import { createCustomElement } from "@servicenow/ui-core";
import react from "@servicenow/ui-renderer-react";
import view from "./view";
import styles from "./styles.scss";
createCustomElement("my-react-component", {
renderer: { type: react },
view,
properties: {
title: { default: "Hello World" },
count: { default: 0 }
},
actionHandlers: {
INCREMENT: ({ state, updateProperties }) => {
updateProperties({ count: state.properties.count + 1 });
}
},
styles
});// my-component/view.js
import React, { useState } from "react";
export default function MyReactComponent(state) {
const { dispatch, helpers, properties } = state;
const { title, count } = properties;
const [localState, setLocalState] = useState('');
const handleClick = () => {
dispatch('INCREMENT');
setLocalState('Button clicked!');
};
return (
<div>
<h1>{title}</h1>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment</button>
<p>{localState}</p>
</div>
);
}How It Works
This package solves the JSX transformation problem in ServiceNow by:
- Installing as a regular npm package (
@quixomatic/ui-renderer-react-simple) - Creating a fake local package (
@servicenow/ui-renderer-react) that tricks the babel plugin - Automatically patching ServiceNow's babel plugin to handle React renderers correctly
- Providing React 18 compatibility while maintaining the same API
- Filtering VNode objects to prevent React rendering errors
What the Setup Script Does
- Creates
src/node_modules/@servicenow/ui-renderer-react/directory - Copies the renderer files to masquerade as the official ServiceNow renderer
- Updates your
package.jsonto include the local dependency - Automatically patches the ServiceNow babel plugin in
~/.snc/.extensions/ui-component/ - Creates a backup of the original babel plugin for easy restoration
- Ensures React 18 dependencies are present
Babel Plugin Patching
The setup script automatically:
- Locates ServiceNow's babel plugin file
- Creates a backup before making changes
- Patches the React renderer configuration to use the correct module path
- Removes any old renderer entries
- Verifies the patch was applied successfully
Manual Babel Operations
If needed, you can manually control the babel plugin:
# Patch just the babel plugin
npx patch-servicenow-babel
# Restore the original babel plugin
npx restore-servicenow-babelMigration from @quixomatic/ui-renderer-react
If you're migrating from the original @quixomatic/ui-renderer-react:
- Remove the old package:
npm uninstall @quixomatic/ui-renderer-react - Install this package:
npm install @quixomatic/ui-renderer-react-simple - Run setup:
npx setup-servicenow-react - Update imports: Change imports to
@servicenow/ui-renderer-react - No other changes needed - your components will work exactly the same
Key Improvements
- No manual babel modifications - everything is automated
- Automatic babel plugin patching - works out of the box
- Easy restoration - restore original babel plugin anytime
- Better error handling - improved setup process with fallbacks
Troubleshooting
"Objects are not valid as a React child" Error
This usually means the setup didn't complete properly:
- Verify the setup script ran successfully
- Check that
@servicenow/ui-renderer-reactappears in yourpackage.json - Ensure
src/node_modules/@servicenow/ui-renderer-react/exists - Run
npm installagain - Try running
npx patch-servicenow-babelseparately
Babel Plugin Issues
If babel patching fails:
- Check that ServiceNow CLI is installed:
snc --version - Verify the babel plugin exists:
npx patch-servicenow-babel - Try restoring and re-patching:
npx restore-servicenow-babel && npx patch-servicenow-babel
React Hooks Not Working
Ensure you're using React 18:
npm list react react-domBoth should show version 18.x.x.
Manual Setup
If the automatic setup fails, you can set up manually:
Create the directory:
mkdir -p src/node_modules/@servicenow/ui-renderer-reactCopy the renderer:
cp node_modules/@quixomatic/ui-renderer-react-simple/index.js src/node_modules/@servicenow/ui-renderer-react/Create package.json:
{ "name": "@servicenow/ui-renderer-react", "version": "2.0.0", "main": "index.js", "peerDependencies": { "react": "^18.0.0", "react-dom": "^18.0.0" } }Add to your package.json:
{ "dependencies": { "@servicenow/ui-renderer-react": "file:src/node_modules/@servicenow/ui-renderer-react" } }Run npm install
Patch babel manually:
npx patch-servicenow-babel
Requirements
- Node.js 16+
- React 18.x
- ServiceNow Next Experience UI Framework
- ServiceNow CLI (
snc)
License
MIT
Contributing
Issues and pull requests are welcome on GitHub.
Support
For support, please open an issue on GitHub or contact the maintainer.
Changelog
v1.1.0
- Added automatic babel plugin patching
- Added CLI commands for patch/restore operations
- Improved setup process with better error handling
- Added backup/restore functionality for babel plugin
v1.0.1
- Initial release with fake package approach
- React 18 compatibility
- VNode filtering
- Error boundaries
