Project setup
Custom Metric widget project setup — main.jsx entry, UbidotsProvider mounting into
A Custom Metric widget is a small React app. Its files are:
main.jsx: the entry point.Widget.jsxand other components (optional).style.css.package.json.manifest.toml.
The compiled output is script.js, plus style.css.
Entry and mounting (main.jsx)
import React from 'react';
import { createRoot } from 'react-dom/client';
import { UbidotsProvider } from '@ubidots/react-html-canvas';
import Widget from './Widget.jsx';
import './style.css';
// In production the widget id comes from the URL (?id=...); in the editor preview the
// host injects window.__UBIDOTS_WIDGET_ID__. Read both, in that order.
const widgetId =
window.__UBIDOTS_WIDGET_ID__ ||
new URLSearchParams(window.location.search).get('id') ||
undefined;
createRoot(document.getElementById('root')).render(
<UbidotsProvider widgetId={widgetId}>
<Widget />
</UbidotsProvider>
);UbidotsProvider props:
widgetId
The widget's id, read as shown above.
readyEvents
The inbound events the widget waits for before it is "ready". Default: ['receivedToken'].
onReady
Called when the widget is ready.
validateOrigin
(origin: string) => boolean, to accept messages only from trusted origins.
initialStateOverride
Initial state, mostly for tests.
The app mounts into the existing <div id="root"></div>, which the bundler generates automatically.
manifest.toml
Declare the compiled assets:
The [page.settings] block holds the widget's metadata. Keep its existing keys.
package.json
Pin "@ubidots/react-html-canvas": "0.3.0", plus react and react-dom. Other packages resolve through esm.sh. Add them only when you need them.
Last updated
Was this helpful?