@reatom/lit package provides integration between Reatom and Lit, enabling reactive state management in web components with automatic dependency tracking.
Installation
This package requires Lit 2.6.0 or higher and is currently in alpha.
Core Features
withReatomElement
Create reactive Lit elements that automatically track atom dependencies:- Automatic subscription management
- Re-renders only when accessed atoms change
- Lifecycle integration (connects/disconnects subscriptions)
- Works with standard Lit decorators and features
html and svg Templates
Reatom provides enhanced template functions that automatically watch atoms:watch directive.
watch Directive
Manually watch atoms in templates:watch directive:
- Subscribes to atom changes
- Updates the template when atom changes
- Automatically unsubscribes when disconnected
- Accepts an optional frame parameter
Complete Examples
Counter Component
Todo List Component
Multi-Component App
SVG with Atoms
TypeScript Support
Full TypeScript support with decorators:Best Practices
1
Always extend with withReatomElement
Use
withReatomElement(LitElement) as your base class to enable automatic atom tracking.2
Use Reatom html and svg
Import template functions from
@reatom/lit for automatic atom watching in templates.3
Share atoms across components
Define atoms outside components to share state across multiple web components.
4
Leverage computed atoms
Use computed atoms for derived state to maintain reactivity across the component tree.
Lifecycle Integration
ThewithReatomElement mixin integrates with Litβs lifecycle:
- connectedCallback: Starts tracking atoms and subscribes to changes
- disconnectedCallback: Unsubscribes from all atoms
- shouldUpdate: Optimized to prevent unnecessary updates
- render: Automatically re-renders when tracked atoms change
Next Steps
- Learn about Core Concepts for atoms and state management
- Explore Web Components for building custom elements
- Check out [Asynchttps://github.com/reatom/reatom/tree/main/packages for handling asynchronous state