Loading...
Loading...
React useEffect anti-pattern detection and correction guide. Use this skill whenever writing, reviewing, or modifying any React component that contains useEffect, or when about to add a useEffect hook. Also trigger when you see patterns like "setState inside useEffect", "effect chains", "derived state in effect", or "notify parent in effect". Covers 12 specific scenarios where Effects are unnecessary or misused, with correct alternatives. Even if the useEffect looks reasonable at first glance, consult this skill to verify it's truly needed.
npx skill4agent add whinc/super-skills react-effectsuseEffectuseEffect// WRONG: Redundant state + unnecessary Effect
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(firstName + ' ' + lastName);
}, [firstName, lastName]);
// RIGHT: Compute during render — zero extra renders, zero extra state
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
const fullName = firstName + ' ' + lastName;useEffectsetSomeState(f(props, state))useMemo// WRONG: Effect + state for caching
const [visibleTodos, setVisibleTodos] = useState([]);
useEffect(() => {
setVisibleTodos(getFilteredTodos(todos, filter));
}, [todos, filter]);
// RIGHT (simple case): Just compute it
const visibleTodos = getFilteredTodos(todos, filter);
// RIGHT (expensive): useMemo skips recomputation when deps haven't changed
const visibleTodos = useMemo(
() => getFilteredTodos(todos, filter),
[todos, filter]
);console.timeconsole.timeEnduserIdkey// WRONG: Effect to reset state on prop change
function ProfilePage({ userId }) {
const [comment, setComment] = useState('');
useEffect(() => {
setComment('');
}, [userId]);
return /* ... */;
}
// RIGHT: key tells React "this is a different component instance"
function ProfilePage({ userId }) {
return <Profile userId={userId} key={userId} />;
}
function Profile({ userId }) {
const [comment, setComment] = useState(''); // Auto-resets when key changes
return /* ... */;
}useEffect(() => { setX(initial); setY(initial); ... }, [someProp])// WRONG: Effect to clear selection when items change
function List({ items }) {
const [selection, setSelection] = useState(null);
useEffect(() => {
setSelection(null);
}, [items]);
return /* ... */;
}
// BETTER: Store the ID, derive the selected object
function List({ items }) {
const [selectedId, setSelectedId] = useState(null);
// If the selected item is still in the list, keep it; otherwise null
const selection = items.find(item => item.id === selectedId) ?? null;
return /* ... */;
}function List({ items }) {
const [prevItems, setPrevItems] = useState(items);
const [selection, setSelection] = useState(null);
if (items !== prevItems) {
setPrevItems(items);
setSelection(null);
}
}// WRONG: Shows notification whenever product.isInCart becomes true
// (including page refresh, back navigation, etc.)
function ProductPage({ product, addToCart }) {
useEffect(() => {
if (product.isInCart) {
showNotification(`Added ${product.name} to cart!`);
}
}, [product]);
function handleBuyClick() {
addToCart(product);
}
}
// RIGHT: Notification is a direct response to user action
function ProductPage({ product, addToCart }) {
function buyProduct() {
addToCart(product);
showNotification(`Added ${product.name} to cart!`);
}
function handleBuyClick() {
buyProduct();
}
function handleCheckoutClick() {
buyProduct();
navigateTo('/checkout');
}
}useEffectshowNotificationnavigatealert[someFlag]// WRONG: Roundabout way to send form data
const [jsonToSubmit, setJsonToSubmit] = useState(null);
useEffect(() => {
if (jsonToSubmit !== null) {
post('/api/register', jsonToSubmit);
}
}, [jsonToSubmit]);
function handleSubmit(e) {
e.preventDefault();
setJsonToSubmit({ firstName, lastName });
}
// RIGHT: Submit directly in the event handler
function handleSubmit(e) {
e.preventDefault();
post('/api/register', { firstName, lastName });
}
// This analytics Effect IS correct — it runs because the component displayed
useEffect(() => {
post('/analytics/event', { eventName: 'visit_form' });
}, []);// WRONG: Chain of Effects triggering each other
useEffect(() => {
if (card !== null && card.gold) {
setGoldCardCount(c => c + 1);
}
}, [card]);
useEffect(() => {
if (goldCardCount > 3) {
setRound(r => r + 1);
setGoldCardCount(0);
}
}, [goldCardCount]);
useEffect(() => {
if (round > 5) {
setIsGameOver(true);
}
}, [round]);
// RIGHT: Derive what you can, compute the rest in the event handler
const isGameOver = round > 5; // Derived, not state
function handlePlaceCard(nextCard) {
if (isGameOver) throw Error('Game already ended.');
setCard(nextCard);
if (nextCard.gold) {
if (goldCardCount < 3) {
setGoldCardCount(goldCardCount + 1);
} else {
setGoldCardCount(0);
setRound(round + 1);
if (round === 5) {
alert('Good game!');
}
}
}
}useEffect// WRONG: Runs twice in StrictMode development, may cause issues
function App() {
useEffect(() => {
loadDataFromLocalStorage();
checkAuthToken();
}, []);
}
// RIGHT (option A): Module-level guard
let didInit = false;
function App() {
useEffect(() => {
if (!didInit) {
didInit = true;
loadDataFromLocalStorage();
checkAuthToken();
}
}, []);
}
// RIGHT (option B): Module-level execution (runs on import, once)
if (typeof window !== 'undefined') {
checkAuthToken();
loadDataFromLocalStorage();
}
function App() {
// ...
}// WRONG: Effect to notify parent — renders twice
function Toggle({ onChange }) {
const [isOn, setIsOn] = useState(false);
useEffect(() => {
onChange(isOn);
}, [isOn, onChange]);
function handleClick() {
setIsOn(!isOn);
}
}
// RIGHT: Update child + notify parent in the same event
// React batches both setState calls into a single render
function Toggle({ onChange }) {
const [isOn, setIsOn] = useState(false);
function updateToggle(nextIsOn) {
setIsOn(nextIsOn);
onChange(nextIsOn); // Parent updates in the same batch
}
function handleClick() {
updateToggle(!isOn);
}
}
// BEST: Fully controlled — no local state at all
function Toggle({ isOn, onChange }) {
function handleClick() {
onChange(!isOn);
}
}useEffect(() => { onSomething(localState); }, [localState, onSomething])// WRONG: Child fetches, then pushes data to parent via Effect
function Parent() {
const [data, setData] = useState(null);
return <Child onFetched={setData} />;
}
function Child({ onFetched }) {
const data = useSomeAPI();
useEffect(() => {
if (data) onFetched(data);
}, [onFetched, data]);
}
// RIGHT: Parent owns the data fetching, passes data down
function Parent() {
const data = useSomeAPI();
return <Child data={data} />;
}navigator.onLine// SUBOPTIMAL: Manual subscription in Effect
function useOnlineStatus() {
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
function update() { setIsOnline(navigator.onLine); }
update();
window.addEventListener('online', update);
window.addEventListener('offline', update);
return () => {
window.removeEventListener('online', update);
window.removeEventListener('offline', update);
};
}, []);
return isOnline;
}
// RIGHT: useSyncExternalStore — purpose-built for this
function subscribe(callback) {
window.addEventListener('online', callback);
window.addEventListener('offline', callback);
return () => {
window.removeEventListener('online', callback);
window.removeEventListener('offline', callback);
};
}
function useOnlineStatus() {
return useSyncExternalStore(
subscribe,
() => navigator.onLine, // Client snapshot
() => true // Server snapshot
);
}// CORRECT: Effect with ignore flag for race condition handling
useEffect(() => {
let ignore = false;
fetchResults(query, page).then(json => {
if (!ignore) {
setResults(json);
}
});
return () => { ignore = true; };
}, [query, page]);| What the Effect does | Alternative |
|---|---|
| Computes a value from props/state | Compute during render (or |
| Resets all state when a prop changes | Add a |
| Adjusts some state when a prop changes | Derive the value instead of storing it |
| Runs code when user clicks/submits | Move to event handler |
| Sends POST request from user action | Move to event handler |
| Sets state that triggers another Effect | Consolidate into one event handler |
| Initializes app once | Module-level code or |
Calls parent's | Call |
| Pushes data from child to parent | Lift data fetching to parent |
| Subscribes to external data source | Use |
| Fetches data | Keep the Effect but add cleanup; prefer TanStack Query |
useEffectuseMemoignoreeslint-disablecomponentDidMountcomponentDidUpdate