Separate non-reactive logic with useEffectEvent
Separate synchronization from notification behavior
An Effect connects to a resource based on reactive inputs. Some callbacks fired by that resource need the newest UI preferences but should not restart the connection whenever those preferences change. useEffectEvent, introduced in React 19.2, provides a way to express that separation. Use a reviewed patched compatible React and React DOM release and compatible Hooks lint rules. React 18 and 19.0/19.1 do not expose this stable API.
import React,{useState,useEffect,useEffectEvent,useSyncExternalStore} from 'react';function createFeed(station){const listeners=new Set();return {subscribe(fn){listeners.add(fn);return ()=>listeners.delete(fn);},emit(){for(const fn of [...listeners])fn(station+' kit ready');}};}const feeds={north:createFeed('North'),south:createFeed('South')};function Reader({station,uppercase}){const [message,setMessage]=useState('Waiting');const report=useEffectEvent(text=>setMessage(uppercase?text.toUpperCase():text));useEffect(()=>feeds[station].subscribe(text=>report(text)),[station]);return <p role="status">{message}</p>;}export default function App(){const [station,setStation]=useState('north');const [upper,setUpper]=useState(false);return <section><label>Station <select value={station} onChange={e=>setStation(e.target.value)}><option value="north">North</option><option value="south">South</option></select></label><label><input type="checkbox" checked={upper} onChange={e=>setUpper(e.target.checked)}/> Uppercase reports</label><button onClick={()=>feeds[station].emit()}>Simulate update</button><Reader key={station} station={station} uppercase={upper}/></section>;}Simulate update displays a message for the selected station. Changing uppercase does not require the reader to reconnect; the next simulated update uses the new preference. The station selects the source, so it remains reactive. The key deliberately resets the displayed message when the station changes. The user handler emits an external fixture event; it does not call the Effect Event directly. The subscribed callback is established inside the owning Effect.
Keep genuine dependencies
import React,{useState,useEffect,useEffectEvent,useSyncExternalStore} from 'react';function Reader({station,compact}){const [notice,setNotice]=useState('Waiting');const report=useEffectEvent(name=>setNotice(compact?name:'Subscribed to '+name));useEffect(()=>{const timer=setTimeout(()=>report(station),0);return ()=>clearTimeout(timer);},[station]);return <p role="status">{notice}</p>;}export default function App(){const [station,setStation]=useState('North');const [compact,setCompact]=useState(false);return <section><button onClick={()=>setStation(s=>s==='North'?'South':'North')}>Switch station</button><button onClick={()=>setCompact(v=>!v)}>Toggle compact mode</button><Reader station={station} compact={compact}/></section>;}This bounded timer simulates a subscription notification. Changing station cancels the prior pending notification and schedules a new one. Changing compact alone does not recalculate an old message: the event reads the preference when the timer fires. If the old message must instantly change presentation, store the raw station and derive its label during render instead. Do not remove station from dependencies by moving the source selection inside an Effect Event.
Know when a normal event handler is enough
import React,{useState,useEffect,useSyncExternalStore} from 'react';export default function App(){const [compact,setCompact]=useState(false);const [message,setMessage]=useState('');function preview(){setMessage(compact?'Ready':'Equipment is ready');}return <section><label><input type="checkbox" checked={compact} onChange={e=>setCompact(e.target.checked)}/> Compact</label><button onClick={preview}>Preview</button><p role="status">{message}</p></section>;}Preview is an ordinary click handler. It already closes over values from the current render. Do not call Effect Events from user handlers or render, pass them to other components or hooks, or add them to dependency arrays. Their identity is not stable. Keep the event with the Effect or custom Hook that owns it. In older React, a correct dependency list and reconnection is often a reasonable simpler alternative. Practice: decide whether the station, message format, and a user-triggered preview should be reactive dependencies, Effect Event inputs, or regular event handlers.