writing-kea-logics
БесплатноБез исполняемых скриптовНе проверенGuide for writing or reviewing PostHog kea logic files (`*Logic.ts` / `*Logic.tsx`). Use when creating a new logic, adding…
от PostHog★ 41 278 звёзд на GitHubОб этом скилле
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.
.agents/code-style.mdis explicit: "Business logic goes in the kea logic file, not a React hook." Hooks are for view concerns.One concept, one source of truth. Pick exactly one of: action-driven reducer, derived selector, async loader. Don't mirror the same value into multiple places.
Prefer listeners over
kea-subscriptions. Subscriptions install a redux subscription that re-runs on every dispatch and is measurably slower. Listen to the action that changed the value instead. See references/reacting-to-changes.md.Generated types are the contract. Every logic has an inline generated
MakeLogicTypeblock above itskea()call. Import a logic type from the logic source file, never from a separate*LogicType.tsfile.Resources that need cleanup go through
cache.disposables. See the using-kea-disposables skill.
Anatomy at a glance
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import * as api from 'products/foo/frontend/generated/api'
export interface FooLogicProps {
fooId: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface fooLogicValues {
name: string
nameUpper: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface fooLogicActions {
setName: (name: string) => { name: string }
}
export type fooLogicType = MakeLogicType<fooLogicValues, fooLogicActions, FooLogicProps>
export const fooLogic = kea<fooLogicType>([
props({} as FooLogicProps),
key((props) => props.fooId),
path((key) => ['scenes', 'foo', 'fooLogic', key]),
connect(() => ({ values: [teamLogic, ['currentTeamId']] })),
actions({ setName: (name: string) => ({ name }) }),
loaders(({ props }) => ({
foo: [null as Foo | null, { loadFoo: async () => api.foosRetrieve(props.fooId) }],
})),
reducers({ name: ['', { setName: (_, { name }) => name }] }),
selectors({ nameUpper: [(s) => [s.name], (name): string => name.toUpperCase()] }),
listeners(({ actions }) => ({
loadFooSuccess: () => {
/* ... */
},
})),
afterMount(({ actions }) => {
actions.loadFoo()
}),
])
Conventional block order: props → key → path → connect → actions → forms → loaders →
reducers → selectors → sharedListeners → listeners → subscriptions (rare) →
windowValues → urlToAction / actionToUrl → afterMount / propsChanged / beforeUnmount.
You almost never need all of those — half a dozen blocks is typical. Pick the ones the logic actually uses and leave the rest out.
Decision flow — pick the right container before you start
Most kea bugs come from picking the wrong container for a piece of state. Work through this before reaching for any builder:
- Does it come from an HTTP call? Use a loader.
- Can it be computed from other state? Use a
selector. - Does an action change it, and does the UI need to re-render when it changes?
Use a
reducer. - Is it a timer, listener, or other disposable resource? Use
cache.disposables— see using-kea-disposables.
cache.foo is an escape hatch for transient flags the UI never reads — reach for it
last, not first. See references/state-decision.md
for the full breakdown.
Pattern index
Each reference covers one job-to-be-done with the pattern shape, why it'
Установить writing-kea-logics в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл writing-kea-logics?
Guide for writing or reviewing PostHog kea logic files (`*Logic.ts` / `*Logic.tsx`). Use when creating a new logic, adding actions/reducers/selectors/listeners/loaders/forms/router bindings, choosing between reducer vs selector vs cache, deciding between listeners and `kea-subscriptions`, wiring React with `useValues`/`useActions`/`BindLogic`, or onboarding to kea conventions. Read keajs.org for upstream API; this skill captures PostHog-specific conventions and idioms.
Как установить скилл writing-kea-logics?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл writing-kea-logics запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic