import{_ as i,o as a,c as n,ae as t}from"./chunks/framework.C8AWLET_.js";const c=JSON.parse('{"title":"🖼️ Component Lifecycle: $.view( )","description":"","frontmatter":{},"headers":[],"relativePath":"api/view.md","filePath":"api/view.md"}'),e={name:"api/view.md"};function l(h,s,p,k,r,o){return a(),n("div",null,[...s[0]||(s[0]=[t(`
$.view( ) The $.view function is a specialized wrapper used to manage the lifecycle of a UI component. It tracks all signals, effects, and DOM elements created within it, providing a single point of destruction to prevent memory leaks.
$.view(renderFn: Function): RuntimeObject| Parameter | Type | Required | Description |
|---|---|---|---|
renderFn | Function | Yes | A function that returns a DOM Node or an array of Nodes. |
Returns: A Runtime object containing:
container: A div (with display: contents) holding the rendered content.destroy(): A function that unmounts the view and cleans up all internal effects/listeners.When you wrap logic in $.view, SigPro creates a "boundary".
const myView = $.view(() => {
const count = $(0);
// This effect is "owned" by this view
$.effect(() => console.log(count()));
return Div([
H1("Internal View"),
Button({ onclick: () => count(c => c + 1) }, "Add")
]);
});
// To show it:
document.body.appendChild(myView.container);
// To kill it (removes from DOM and stops all internal effects):
myView.destroy();onCleanup The renderFn receives a helper object that allows you to register custom cleanup logic (like closing a WebSocket or a third-party library instance).
const ChartComponent = () => $.view(({ onCleanup }) => {
const socket = new WebSocket("ws://...");
onCleanup(() => {
socket.close();
console.log("Cleanup: WebSocket closed");
});
return Div({ class: "chart-container" });
});When $.view is called, it performs the following:
Set of cleanups.$.effect or child $.view created during execution.display: contents element (which doesn't affect your CSS layout).destroy() is called, it recursively calls all cleanup functions and removes the container from the DOM.$.view? $.router) uses $.view internally to swap pages and clean up the previous one automatically.