308 Rerender Derived State 1c326bb6

Subscribe to Derived State

tools-only Updated 7 repo stars

File contents

Subscribe to Derived State

Subscribe to derived boolean state instead of continuous values to reduce re-render frequency.

Incorrect (re-renders on every pixel change):

function Sidebar() {
  const width = useWindowWidth()  // updates continuously
  const isMobile = width < 768
  return <nav className={isMobile ? 'mobile' : 'desktop'} />
}

Correct (re-renders only when boolean changes):

function Sidebar() {
  const isMobile = useMediaQuery('(max-width: 767px)')
  return <nav className={isMobile ? 'mobile' : 'desktop'} />
}

tools-only/X-Skills/tree/main/automation/scripting/308-rerender-derived-state_1c326bb6 commit e5c899f6c0

Frequently asked questions

npx skillmds@latest add tools-only/308-rerender-derived-state-1c326bb6