Component
TheComponent class is the base class for creating stateful components in GlyphUI. It provides lifecycle methods, state management, and event handling.
Constructor
Parameters
Properties passed to the component from the parent.
Component configuration options.
The initial state for the component.
Instance Properties
props(object): The properties passed to the componentstate(object): The component’s internal statevdom(object): The current virtual DOM representationparentEl(HTMLElement): The parent DOM elementisMounted(boolean): Whether the component is currently mountedslotContents(object): Extracted slot content from children
Methods
setState()
Updates the component’s state and triggers a re-render. Signature:Either an object to merge with the current state, or a function that receives the current state and returns the new state.
emit()
Emits an event to the component’s internal dispatcher. Signature:The name of the event to emit.
The payload to pass with the event.
render()
Renders the component’s virtual DOM. Must be overridden by subclasses. Signature:The component’s props.
The component’s current state.
The emit function for dispatching events.
mount()
Mounts the component to a DOM element. Signature:The parent element to mount to.
unmount()
Unmounts the component and cleans up resources. Signature:updateProps()
Updates the component with new props and re-renders. Signature:New properties to merge with existing props.
Lifecycle Methods
Override these methods in your component to hook into the component lifecycle:beforeMount()
Called before the component is mounted to the DOM.mounted()
Called after the component is mounted to the DOM.beforeUpdate()
Called before the component updates due to prop changes. Signature:The previous props.
The new props.
updated()
Called after the component updates due to prop changes. Signature:The previous props.
The new props.
beforeUnmount()
Called before the component is unmounted.unmounted()
Called after the component is unmounted.Complete Example
Usage
Notes
- Components must override the
render()method, or an error will be thrown - State updates via
setState()automatically trigger a re-render - Lifecycle methods are optional and only called if defined
- The component automatically handles slot content from
props.children