mirror of
https://github.com/The-Low-Code-Foundation/OpenNoodl.git
synced 2026-01-11 14:52:55 +01:00
3.1 KiB
3.1 KiB
TASK-011: React Event Pattern Guide Documentation
Status: ✅ COMPLETED
Summary
Document the React + EventDispatcher pattern in all relevant locations so future developers follow the correct approach and avoid the silent subscription failure pitfall.
Changes Made
1. Created GOLDEN-PATTERN.md ✅
Location: dev-docs/tasks/phase-0-foundation-stabalisation/TASK-011-react-event-pattern-guide/GOLDEN-PATTERN.md
Comprehensive pattern guide including:
- Quick start examples
- Problem explanation
- API reference
- Common patterns
- Debugging guide
- Anti-patterns to avoid
- Migration examples
2. Updated .clinerules ✅
File: .clinerules (root)
Added React + EventDispatcher section:
## Section: React + EventDispatcher Integration
### CRITICAL: Always use useEventListener hook
When subscribing to EventDispatcher events from React components, ALWAYS use the `useEventListener` hook.
Direct subscriptions silently fail.
**✅ CORRECT:**
import { useEventListener } from '@noodl-hooks/useEventListener';
useEventListener(ProjectModel.instance, 'componentRenamed', (data) => {
// This works!
});
**❌ BROKEN:**
useEffect(() => {
const context = {};
ProjectModel.instance.on('event', handler, context);
return () => ProjectModel.instance.off(context);
}, []);
// Compiles and runs without errors, but events are NEVER received
### Why this matters
EventDispatcher uses context-object cleanup pattern incompatible with React closures.
Direct subscriptions fail silently - no errors, no events, just confusion.
### Available dispatchers
- ProjectModel.instance
- NodeLibrary.instance
- WarningsModel.instance
- EventDispatcher.instance
- UndoQueue.instance
### Full documentation
See: dev-docs/tasks/phase-0-foundation-stabalisation/TASK-011-react-event-pattern-guide/GOLDEN-PATTERN.md
3. Updated LEARNINGS.md ✅
File: dev-docs/reference/LEARNINGS.md
Added entry documenting the discovery and solution.
Documentation Locations
The pattern is now documented in:
- Primary Reference:
GOLDEN-PATTERN.md(this directory) - AI Instructions:
.clinerules(root) - Section on React + EventDispatcher - Institutional Knowledge:
dev-docs/reference/LEARNINGS.md - Investigation Details:
TASK-008-eventdispatcher-react-investigation/
Success Criteria
- GOLDEN-PATTERN.md created with comprehensive examples
- .clinerules updated with critical warning and examples
- LEARNINGS.md updated with pattern entry
- Pattern is searchable and discoverable
- Clear anti-patterns documented
For Future Developers
When working with EventDispatcher from React components:
- Search first:
grep -r "useEventListener" .clinerules - Read the pattern:
GOLDEN-PATTERN.mdin this directory - Never use direct
.on()in React: It silently fails - Follow the examples: Copy from GOLDEN-PATTERN.md
Time Spent
Actual: ~1 hour (documentation writing and organization)
Next Steps
- TASK-012: Create health check script to validate patterns automatically
- Use this pattern in all future React migrations
- Update existing components that use broken patterns