Writing kea logics
PostHog uses kea as the state container for the frontend. Almost
all non-trivial business logic lives in a *Logic.ts / *Logic.tsx file, not in
React. We may be on a kea pre-release ahead of the version the keajs.org docs
cover — when in doubt, check pnpm-workspace.yaml for the pinned version.
This skill captures the PostHog-specific conventions on top of the upstream
kea docs. When in doubt about a builder's signature, go upstream.
When in doubt about whether to use it, read here.
Use this skill when
- Creating a new
*Logic.ts/*Logic.tsxfile - Adding builders to an existing logic (actions, reducers, selectors, listeners, loaders, forms)
- Choosing between
reducervsselectorvscachevsloaderfor a piece of state - Wiring a React component to a logic
- Reviewing a PR that introduces or modifies a kea logic
- Reviewing code that uses React hooks where a logic would be more idiomatic
Companion skills (do not duplicate)
- using-kea-disposables —
setInterval,addEventListener,
and any other resource that needs cleanup.
If your work overlaps it, read the companion skill first.
Core principles
- Business logic lives in a logic, not in a component.