home / skills / dust-tt / dust / dust-writing-react-effects
This skill guides writing React components to avoid unnecessary useEffect by deriving state during render and avoiding effect-driven updates.
npx playbooks add skill dust-tt/dust --skill dust-writing-react-effectsReview the files below or copy the command above to add this skill to your agents.
---
name: writing-react-effects
description: Writes React components without unnecessary useEffect. Use when creating/reviewing React components, refactoring effects, or when code uses useEffect to transform data or handle events.
---
# Writing React Effects Skill
Guides writing React components that avoid unnecessary `useEffect` calls.
## Core Principle
> Effects are an escape hatch for synchronizing with **external systems** (network, DOM, third-party widgets). If there's no external system, you don't need an Effect.
## Calculate Derived State During Rendering
If a value can be computed from current props/state, do not store it in state or update it in an effect. Derive it during render to avoid extra renders and state drift. Do not set state in effects solely in response to prop changes; prefer derived values or keyed resets instead.
**Incorrect (redundant state and effect):**
```tsx
function Form() {
const [firstName, setFirstName] = useState('First')
const [lastName, setLastName] = useState('Last')
const [fullName, setFullName] = useState('')
useEffect(() => {
setFullName(firstName + ' ' + lastName)
}, [firstName, lastName])
return <p>{fullName}</p>
}
```
**Correct (derive during render):**
```tsx
function Form() {
const [firstName, setFirstName] = useState('First')
const [lastName, setLastName] = useState('Last')
const fullName = firstName + ' ' + lastName
return <p>{fullName}</p>
}
```
References: [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect)
This skill helps you write and review React components that avoid unnecessary useEffect hooks. It emphasizes deriving values during render and reserving effects for true side effects that interact with external systems. The goal is fewer renders, less state drift, and clearer component logic.
The skill inspects component code to find patterns where state is set inside useEffect only to mirror props or other state. It flags redundant state and suggests deriving values directly in render or using keyed resets when appropriate. It also highlights true side effects (network, DOM, subscriptions) so you keep useEffect when it is actually needed.
How do I tell if an effect is necessary?
Ask whether the effect interacts with an external system (network, DOM, timers, subscriptions). If it only transforms props/state, compute the result during render instead.
What if deriving during render is expensive?
Use memoization (useMemo) for expensive calculations, but only when cost justifies it. Prefer keeping logic pure and avoid state duplication unless performance profiling shows a problem.