ReactLynx Best Practices
Use this skill when writing, reviewing, or refactoring ReactLynx code. ReactLynx follows the React programming model, but Lynx's dual-thread runtime changes how side effects, lifecycle timing, event handlers, and main-thread scripts should be reasoned about.
This skill intentionally does not require @ast-grep/napi or any native parser at runtime. The bundled scanner is a lightweight heuristic helper for common issues. The agent must still read the code and apply the rule documents in rules/*.md.
When to Apply
- Writing new ReactLynx components or application code.
- Publishing or reviewing reusable ReactLynx component libraries.
- Reviewing ReactLynx code for thread-boundary, lifecycle, event, lynx.__globalProps, code-splitting, or performance issues.
- Refactoring code that calls
lynx.getJSModule, NativeModules, runOnMainThread, runOnBackground, lazy, Suspense, or useLayoutEffect.
- Investigating performance traces that include ReactLynx render, diff, commit, patch, or setState events.
Official References
Workflow
1. Classify the task
Use one of these modes:
| Mode |
Use when |
writing |
The user asks for new ReactLynx code or best-practice guidance |
review |
The user asks to check, audit, explain, or validate existing code |
refactor |
The user asks to fix or rewrite existing code |
If the mode is not explicit, infer it from the user's wording. Prefer review before refactor when code has not been inspected yet.
2. Inspect the code
For repository work, search before editing:
rg "lynx.getJSModule|NativeModules|useLayoutEffect|main-thread:|runOnMainThread|runOnBackground|lazy\\(|Suspense|background only|globalPropsMode|__globalProps|jsx|exports" <target>
Read nearby components, custom hooks, custom components that forward event handlers, Rspeedy config, and performance-related code before making changes.
3. Run the helper scanner when source is available
The scanner catches common background-only and lifecycle issues. It is not a complete parser and must not replace code review.
node -e "
import fs from 'fs';
import { ReactLynxWorkflow, formatScanReport } from '<path_to_skill>/scripts/index.mjs';
const input = '<sourceCodeOrFilePath>';
const sourceCode = fs.existsSync(input) ? fs.readFileSync(input, 'utf-8') : input;
const workflow = new ReactLynxWorkflow('review');
const summary = workflow.reviewCode(sourceCode);
console.log(formatScanReport(summary));
"
4. Apply the rule checklist
Always combine scanner output with these manual checks:
- Dual-thread boundaries: render code may run on the main thread; side effects and native APIs must be background-only.
- Background-only propagation: code called only from background-only code is background-only, but custom prop and custom hook boundaries often need an explicit
'background only' directive.
- Lifecycle:
useLayoutEffect is unsupported; use useEffect for background side effects or main-thread layout events/refs for layout reads.
- Events: normal
bind*/catch* handlers run on the background thread; main-thread:* handlers require 'main thread' and have stricter limitations.
- MTS: captured values must be JSON-serializable, captured variables cannot be modified, nested main-thread functions are unsupported, and cross-thread calls must use
runOnMainThread() or runOnBackground().
- Shared modules: import helpers with
with { runtime: 'shared' } only for code sharing, not state sharing.
- Component libraries: publish type-erased
dist ESM with authored JSX preserved in JSX-bearing .jsx files and matching declarations; export the actual .js or .jsx entry that the build emits. Expose TS/TSX source only through an explicit source field or supported condition. Check Rslib, TypeScript, Babel, and SWC output for classic, automatic, or custom-factory JSX lowering. Treat intentional React.createElement according to the @lynx-js/react peer range instead of assuming every call is incompatible.
lynx.__globalProps: Host-injected cross-page/global data updated through updateGlobalProps.
globalPropsMode: 'reactive' triggers root forceUpdate; 'event' requires explicit updates with useGlobalPropsChanged. When migrating to 'event', scan direct lynx.__globalProps reads because root forceUpdate no longer applies.
- Code splitting: lazy components need default exports,
Suspense, CSS scope awareness, and error handling for important boundaries.
- Profiling: use trace events and readable
displayName values to identify hot render/diff/update paths before optimizing.
5. Refactor safely
For refactor mode:
- Report current findings first.
- Explain which fixes are mechanical and which require human design judgment.
- Apply only scoped changes.
- Re-run the helper scanner or package tests after edits.
Use auto-fixes only as suggestions. The current auto-fixes are designed for detect-background-only diagnostics and should be reviewed before applying.
node -e "
import fs from 'fs';
import { ReactLynxWorkflow, formatFixPlan } from '<path_to_skill>/scripts/index.mjs';
const input = '<sourceCodeOrFilePath>';
const sourceCode = fs.existsSync(input) ? fs.readFileSync(input, 'utf-8') : input;
const workflow = new ReactLynxWorkflow('refactor');
workflow.reviewCode(sourceCode);
const plan = workflow.generateFixPlan();
if (plan) {
console.log(formatFixPlan(plan));
}
"
Rules
| Rule |
Impact |
Use for |
| detect-background-only |
CRITICAL |
lynx.getJSModule, NativeModules, 'background only', custom event/hook boundaries |
| avoid-use-layout-effect |
MEDIUM |
Lifecycle and layout reads |
| proper-event-handlers |
MEDIUM |
bindtap, catchtap, propagation, dataset, custom prop handlers |
| main-thread-scripts-guide |
MEDIUM |
main-thread:*, useMainThreadRef, cross-thread calls, shared modules |
| component-library-packaging |
HIGH |
ReactLynx component-library exports, type-erased ESM, preserved JSX, Rslib, tsc |
| global-props-mode |
MEDIUM |
globalPropsMode config, direct lynx.__globalProps reads, useGlobalPropsChanged migration |
| code-splitting |
MEDIUM |
lazy, Suspense, standalone lazy bundles, CSS bundle scope |
| performance-profiling |
MEDIUM |
ReactLynx trace events, flow IDs, displayName |
| hoist-static-jsx |
LOW |
Static JSX and render cost |
API Reference
function runSkill(source: string): Diagnostic[];
function runSkillWithFixes(source: string): DiagnosticWithFix[];
function analyzeBackgroundOnlyUsage(source: string): Diagnostic[];
function analyzeLifecycleUsage(source: string): Diagnostic[];
function generateFixes(source: string, diagnostic: Diagnostic): Fix[];
function applyFix(source: string, fix: Fix): string;
function applyFixes(source: string, fixes: Fix[]): string;
function formatScanReport(summary: ScanSummary): string;
function formatFixPlan(plan: FixPlan): string;
class ReactLynxWorkflow {
constructor(mode: WorkflowMode);
reviewCode(source: string): ScanSummary;
generateFixPlan(): FixPlan | null;
applyAutoFixes(source: string): { fixed: string; appliedFixes: Fix[] };
}
1---2name: reactlynx-best-practices3description: Reviews, writes, and refactors ReactLynx code and component libraries for Lynx dual-thread best practices. Applies when writing ReactLynx components, or handling background-only, useLayoutEffect, bindtap/catchtap, main-thread:*, runOnMainThread/runOnBackground, lazy/Suspense, globalPropsMode/__globalProps, component-library publishing (preserved JSX vs React.createElement), or render/diff/commit performance traces. Excludes vanilla Lynx Element PAPI without ReactLynx JSX (use vanilla-lynx), running-app debugging via DevTool/CDP (use lynx-devtool), or Rspeedy/tsconfig config (use lynx-typescript).4---5
6# ReactLynx Best Practices
7
8Use this skill when writing, reviewing, or refactoring ReactLynx code. ReactLynx follows the React programming model, but Lynx's dual-thread runtime changes how side effects, lifecycle timing, event handlers, and main-thread scripts should be reasoned about.
9
10This skill intentionally does not require `@ast-grep/napi` or any native parser at runtime. The bundled scanner is a lightweight heuristic helper for common issues. The agent must still read the code and apply the rule documents in `rules/*.md`.
11
12## When to Apply
13
14- Writing new ReactLynx components or application code.
15- Publishing or reviewing reusable ReactLynx component libraries.
16- Reviewing ReactLynx code for thread-boundary, lifecycle, event, lynx.__globalProps, code-splitting, or performance issues.
17- Refactoring code that calls `lynx.getJSModule`, `NativeModules`, `runOnMainThread`, `runOnBackground`, `lazy`, `Suspense`, or `useLayoutEffect`.
18- Investigating performance traces that include ReactLynx render, diff, commit, patch, or setState events.
19
20## Official References
21
22- Thinking in ReactLynx: https://lynxjs.org/next/react/thinking-in-reactlynx.html
23- Rendering Process and Lifecycle: https://lynxjs.org/next/react/lifecycle.html
24- Main Thread Script: https://lynxjs.org/next/react/main-thread-script.html
25- Code Splitting: https://lynxjs.org/next/react/code-splitting.html
26- Performance Profiling: https://lynxjs.org/next/react/performance/profiling
27- lynx.__globalProps: https://lynxjs.org/next/api/lynx-api/lynx/lynx-global-props.html
28- globalPropsMode: https://lynxjs.org/next/zh/api/rspeedy/react-rsbuild-plugin.pluginreactlynxoptions.globalpropsmode.html
29- Rslib React and preserved JSX: https://rslib.rs/guide/solution/react
30- TypeScript JSX emit modes: https://www.typescriptlang.org/docs/handbook/jsx
31- ReactLynx 0.121.0 `React.createElement` support: https://github.com/lynx-family/lynx-stack/blob/main/packages/react/CHANGELOG.md#01210
32
33## Workflow
34
35### 1. Classify the task
36
37Use one of these modes:
38
39| Mode | Use when |
40|------|----------|
41| `writing` | The user asks for new ReactLynx code or best-practice guidance |
42| `review` | The user asks to check, audit, explain, or validate existing code |
43| `refactor` | The user asks to fix or rewrite existing code |
44
45If the mode is not explicit, infer it from the user's wording. Prefer `review` before `refactor` when code has not been inspected yet.
46
47### 2. Inspect the code
48
49For repository work, search before editing:
50
51```bash
52rg "lynx.getJSModule|NativeModules|useLayoutEffect|main-thread:|runOnMainThread|runOnBackground|lazy\\(|Suspense|background only|globalPropsMode|__globalProps|jsx|exports" <target>
53```
54
55Read nearby components, custom hooks, custom components that forward event handlers, Rspeedy config, and performance-related code before making changes.
56
57### 3. Run the helper scanner when source is available
58
59The scanner catches common background-only and lifecycle issues. It is not a complete parser and must not replace code review.
60
61```bash
62node -e "
63import fs from 'fs';
64import { ReactLynxWorkflow, formatScanReport } from '<path_to_skill>/scripts/index.mjs';
65
66const input = '<sourceCodeOrFilePath>';
67const sourceCode = fs.existsSync(input) ? fs.readFileSync(input, 'utf-8') : input;
68const workflow = new ReactLynxWorkflow('review');
69const summary = workflow.reviewCode(sourceCode);
70console.log(formatScanReport(summary));
71"
72```
73
74### 4. Apply the rule checklist
75
76Always combine scanner output with these manual checks:
77
78- Dual-thread boundaries: render code may run on the main thread; side effects and native APIs must be background-only.
79- Background-only propagation: code called only from background-only code is background-only, but custom prop and custom hook boundaries often need an explicit `'background only'` directive.
80- Lifecycle: `useLayoutEffect` is unsupported; use `useEffect` for background side effects or main-thread layout events/refs for layout reads.
81- Events: normal `bind*`/`catch*` handlers run on the background thread; `main-thread:*` handlers require `'main thread'` and have stricter limitations.
82- MTS: captured values must be JSON-serializable, captured variables cannot be modified, nested main-thread functions are unsupported, and cross-thread calls must use `runOnMainThread()` or `runOnBackground()`.
83- Shared modules: import helpers with `with { runtime: 'shared' }` only for code sharing, not state sharing.
84- Component libraries: publish type-erased `dist` ESM with authored JSX preserved in JSX-bearing `.jsx` files and matching declarations; export the actual `.js` or `.jsx` entry that the build emits. Expose TS/TSX source only through an explicit source field or supported condition. Check Rslib, TypeScript, Babel, and SWC output for classic, automatic, or custom-factory JSX lowering. Treat intentional `React.createElement` according to the `@lynx-js/react` peer range instead of assuming every call is incompatible.
85- `lynx.__globalProps`: Host-injected cross-page/global data updated through `updateGlobalProps`.
86- `globalPropsMode`: `'reactive'` triggers root `forceUpdate`; `'event'` requires explicit updates with `useGlobalPropsChanged`. When migrating to `'event'`, scan direct `lynx.__globalProps` reads because root `forceUpdate` no longer applies.
87- Code splitting: lazy components need default exports, `Suspense`, CSS scope awareness, and error handling for important boundaries.
88- Profiling: use trace events and readable `displayName` values to identify hot render/diff/update paths before optimizing.
89
90### 5. Refactor safely
91
92For refactor mode:
93
941. Report current findings first.
952. Explain which fixes are mechanical and which require human design judgment.
963. Apply only scoped changes.
974. Re-run the helper scanner or package tests after edits.
98
99Use auto-fixes only as suggestions. The current auto-fixes are designed for `detect-background-only` diagnostics and should be reviewed before applying.
100
101```bash
102node -e "
103import fs from 'fs';
104import { ReactLynxWorkflow, formatFixPlan } from '<path_to_skill>/scripts/index.mjs';
105
106const input = '<sourceCodeOrFilePath>';
107const sourceCode = fs.existsSync(input) ? fs.readFileSync(input, 'utf-8') : input;
108const workflow = new ReactLynxWorkflow('refactor');
109workflow.reviewCode(sourceCode);
110const plan = workflow.generateFixPlan();
111
112if (plan) {
113 console.log(formatFixPlan(plan));
114}
115"
116```
117
118## Rules
119
120| Rule | Impact | Use for |
121|------|--------|---------|
122| [detect-background-only](./rules/detect-background-only.md) | CRITICAL | `lynx.getJSModule`, `NativeModules`, `'background only'`, custom event/hook boundaries |
123| [avoid-use-layout-effect](./rules/avoid-use-layout-effect.md) | MEDIUM | Lifecycle and layout reads |
124| [proper-event-handlers](./rules/proper-event-handlers.md) | MEDIUM | `bindtap`, `catchtap`, propagation, dataset, custom prop handlers |
125| [main-thread-scripts-guide](./rules/main-thread-scripts-guide.md) | MEDIUM | `main-thread:*`, `useMainThreadRef`, cross-thread calls, shared modules |
126| [component-library-packaging](./rules/component-library-packaging.md) | HIGH | ReactLynx component-library exports, type-erased ESM, preserved JSX, Rslib, `tsc` |
127| [global-props-mode](./rules/global-props-mode.md) | MEDIUM | `globalPropsMode` config, direct `lynx.__globalProps` reads, `useGlobalPropsChanged` migration |
128| [code-splitting](./rules/code-splitting.md) | MEDIUM | `lazy`, `Suspense`, standalone lazy bundles, CSS bundle scope |
129| [performance-profiling](./rules/performance-profiling.md) | MEDIUM | ReactLynx trace events, flow IDs, displayName |
130| [hoist-static-jsx](./rules/hoist-static-jsx.md) | LOW | Static JSX and render cost |
131
132## API Reference
133
134```typescript
135function runSkill(source: string): Diagnostic[];
136function runSkillWithFixes(source: string): DiagnosticWithFix[];
137function analyzeBackgroundOnlyUsage(source: string): Diagnostic[];
138function analyzeLifecycleUsage(source: string): Diagnostic[];
139function generateFixes(source: string, diagnostic: Diagnostic): Fix[];
140function applyFix(source: string, fix: Fix): string;
141function applyFixes(source: string, fixes: Fix[]): string;
142function formatScanReport(summary: ScanSummary): string;
143function formatFixPlan(plan: FixPlan): string;
144```
145
146```typescript
147class ReactLynxWorkflow {
148 constructor(mode: WorkflowMode);
149 reviewCode(source: string): ScanSummary;
150 generateFixPlan(): FixPlan | null;
151 applyAutoFixes(source: string): { fixed: string; appliedFixes: Fix[] };
152}
153```