This document outlines the patterns, conventions, and tools used in this codebase to help AI agents work effectively.
This is a lightweight React utility component library providing a ChangesListener component and usePrevious hook. It is written in TypeScript and built with tsup.
| Task | Command | Description |
|---|---|---|
| Build | pnpm run build |
Builds the library using tsup to dist/ (ESM & CJS formats) |
| Test | pnpm test |
Runs tests using vitest |
| Lint/Format | Not explicitly configured | Follow existing code style |
- Package Manager:
pnpm(preferred), but standardnpmscripts work. - Environment: Node.js,
jsdomfor testing.
- src/: Source code.
ChangesListener.tsx: Main component and utility hooks.
- tests/: Test files.
ChangesListener.test.tsx: Component integration tests.setup.ts: Vitest setup (cleanup, jest-dom).
- dist/: Build output (gitignored).
- Strict Mode:
strict: trueis enabled intsconfig.json. - Components: Functional components using hooks.
- Generics: Heavy use of generics (
<T>,<K>) to ensure type safety for watched values. - Null Render: The main component
ChangesListenerreturnsnullas it is a logic-only component.
- Framework:
vitestwith@testing-library/react. - Pattern:
- Use
renderHookfor testing hooks. - Use
renderandactfor testing component behavior/effects. - Mock listeners using
vi.fn(). - Clean up is handled automatically via
tests/setup.ts.
- Use
- Effect-based Logic: The component uses
useRefto track previous values and triggers callbacks in the render phase or effects when comparisons fail. - Type Guards: Checks like
typeof field !== "undefined"are used to discriminate between object property watching and full value watching.
- Exports:
package.jsondefines exports forimport(ESM) andrequire(CJS). Ensure new files are exported correctly if added. - No UI: This library intentionally renders nothing (
return null). Do not add UI elements to the main component.