Skip to main content
The @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:
Benefits:
  • 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:
When you use atoms directly in templates, they’re automatically converted to watched values using the watch directive.

watch Directive

Manually watch atoms in templates:
The 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

The withReatomElement 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