Component System
Component System allows creating reusable elements with unique IDs and client-side functionality through ComponentTemplate.
Component Features
Components are special NodeProxies that provide:
Unique instance IDs
Client-side integration
Props validation
TypeScript support
Scoped styling
Basic Component Example
Component Instance IDs
Each component instance gets unique ID for client-side integration:
Client Integration
Components can include scoped client-side code:
TypeScript Support
Components support full TypeScript typing:
Functions vs Components
You can create reusable elements using both regular functions and components:
Key Differences
Instance Tracking
Functions: No instance tracking
Components: ✓ Unique ID for each instance
1// Function - no ID2button({ onClick: () => {} });3// <div class="button">Click me</div>45// Component - has ID6<Button onClick={() => {}} />;7// <div __rcc="button-1" class="button">Click me</div>Client Integration
Functions: Manual ID management needed
Components: ✓ Automatic ID linking
1// Function - manual ID management2function card() {3 ​const id = generateId(); // Need custom solution4 ​return div({ id })`5 ​ ​<script>6 ​ ​ ​const root = document.getElementById('${id}');7 ​ ​</script>8 ​`;9}1011// Component - automatic ID management12const Card = component((props) => (13 ​<div>14 ​ ​<script data-component={props.__rcc}>15 ​ ​ ​const root = document.querySelector('[__rcc="${props.__rcc}"]');16 ​ ​</script>17 ​</div>18));Chainability and Reuse
Functions: New instance on each call
Components: ✓ Can be chained and reused like NodeProxies
1// Function - creates new element each time2const base = button({ class: "primary" });3const withSize = button({ class: "primary", size: "large" }); // New instance45// Component - can be chained like NodeProxies6const Base = <Button class="primary" />;7const WithSize = Base({ size: "large" }); // Chains from base8const WithText = Base`Click me`; // Chains from baseRender Timing
Functions: Execute during template creation
Components: ✓ Execute during render phase
1// Function - executes immediately2const result = button({ id: "btn-1" }); // Creates element now34// Component - executes during render5const Button = component((props) => {6 ​console.log("Rendering button"); // Logs during render7 ​return button(props);8});9const result = <Button id="btn-1" />; // Just creates template
When to Use What
Use Functions when:
No client-side code needed
Simple reusable elements
No instance tracking required
Performance is critical
Use Components when:
Client-side integration needed
Instance tracking required
Props validation needed
Complex reusable elements
Working with other components
Best Practices
Start with functions for simple elements
Convert to components when needed:
Adding client code
Need instance tracking
Use components for app architecture
Use functions for utility elements