Decorators
Ignite-Element provides powerful @Shared and @Isolated decorators for building modular, scalable web components. These decorators streamline integrating state management with rendering logic, allowing you to create maintainable and reusable components.
Enable Decorators in TypeScript
To use decorators in your project, make sure the following options are enabled in your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Without these settings, TypeScript will not recognize decorators in your code.
Type Safety with RenderArgs
RenderArgsIgnite-Element provides the RenderArgs type helper to make your components type-safe when using decorators. You’ll need to pass typeof source (e.g., your state machine or store) to RenderArgs to ensure type inference.
Future Plans: We aim to infer types automatically for Ignite-Element decorators in the future, simplifying the setup further.
What Are Decorators
@Shared: Creates components that share the same state across all instances. Ideal for global states like progress bars, dashboards, or game stats.@Isolated: Creates components with independent states for each instance. Perfect for localized states like tab navigation, rating systems, or dynamic forms.
Shared Decorator
Overview
The @Shared decorator connects a component to a global state, ensuring all component instances reflect and update the same state.
Example: Progress Bar Component
The following example demonstrates a progress bar that calculates the percentage of completed tasks based on the global state managed by a state machine.
Isolated Decorator
Overview
The @Isolated decorator connects a component to a state that is independent for each instance. This ensures that multiple component instances can coexist with unique behaviors and states.
Example 2: Rating Component
The RatingItem component tracks its rating state independently for each instance.
Best Practices
Centralize Logic:
Use state machines or stores to manage all data and interactions.
Choose the Right Decorator:
Use
@Sharedfor global states (e.g., dashboards, progress bars).Use
@Isolatedfor independent states (e.g., tabs, ratings).
Dynamic Contexts:
Configure state machines dynamically for greater flexibility.
Encapsulate Rendering:
Keep rendering logic focused and delegate state management to the source.
Last updated