Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/rerender-derived-state-no-effect.md
1---2title: Calculate Derived State During Rendering3impact: MEDIUM4impactDescription: avoids redundant renders and state drift5tags: rerender, derived-state, useEffect, state6---78## Calculate Derived State During Rendering910If 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.1112**Incorrect (redundant state and effect):**1314```tsx15function Form() {16const [firstName, setFirstName] = useState('First')17const [lastName, setLastName] = useState('Last')18const [fullName, setFullName] = useState('')1920useEffect(() => {21setFullName(firstName + ' ' + lastName)22}, [firstName, lastName])2324return <p>{fullName}</p>25}26```2728**Correct (derive during render):**2930```tsx31function Form() {32const [firstName, setFirstName] = useState('First')33const [lastName, setLastName] = useState('Last')34const fullName = firstName + ' ' + lastName3536return <p>{fullName}</p>37}38```3940References: [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect)41