1function Editor() {
2 const [content, setContent] = useState('');
3
4 useEffect(() => {
5 function handleSave(e) {
6 if (e.ctrlKey && e.key === 's') {
7 saveToServer(content);
8 }
9 }
10 window.addEventListener('keydown', handleSave);
11 }, []);
12
13 return <textarea value={content} onChange={e => setContent(e.target.value)} />;
14}
no lines flagged
#051PracticeMedium20 min · 120 XP
Stale Closure in Event Listener
A keyboard shortcut handler always uses the initial state value because it is registered once without cleanup.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.