SolidJS component lifecycle differs significantly from React:
Core Characteristics:
- Component function executes only once, not re-executed on props changes
- Use
onMountfor component mounting - Use
onCleanupfor cleanup logic - Use
createEffectfor reactive updates
Lifecycle Hooks:
javascriptfunction MyComponent(props) { // Component initialization (executes once) const [count, setCount] = createSignal(0); // Execute after mount onMount(() => { console.log('Component mounted'); }); // Reactive side effect createEffect(() => { console.log('Count changed:', count()); }); // Cleanup logic onCleanup(() => { console.log('Cleanup'); }); return <div>{count()}</div>; }
Comparison with React:
| Feature | React | SolidJS |
|---|---|---|
| Component execution | Every render | Once only |
| State update | Triggers re-render | Fine-grained DOM update |
| useEffect | Needs dependency array | Auto-tracks dependencies |
| Component unmount | useEffect cleanup | onCleanup |
Best Practices:
- Put reactive logic in
createEffect - Use
onMountfor one-time initialization - Use
onCleanupto clean up side effects - Avoid calling signal getters directly in component function