To allow other widgets to interact with your widget programmatically (beyond just syncing model state), you can return an object from the initialize function. This object becomes the widget's exports.
These exports are made available to parent widgets via the host.getWidget(ref) method.
Note on return types:
- Returning
void: No exports. - Returning
() => void: A cleanup callback (legacy). - Returning
object: The widget's public interface (exports).
If you need to return a single function as your export, wrap it in an object (e.g., { call: fn }) to avoid being interpreted as a legacy cleanup callback.
export default () => {
let data;
return {
initialize({ model, signal }) {
data = buildReactiveStore(model, { signal });
// This object is the widget's exports
return {
getValue: () => data.current,
setValue: (x) => data.set(x),
subscribe: (cb) => data.subscribe(cb),
};
},
render({ model, el, signal }) {
// uses `data` via closure
},
};
};