Chapter 2 · Practice 3 of 5
Read screen size with useWindowDimensions, not at module scope
Why it matters
Phones rotate, tablets split the screen and foldables change size while your app is open. A width read once at import time with Dimensions.get('window') goes stale and your layout breaks.
- Use
useWindowDimensions()inside components so they re-render when the size changes. - Prefer flexbox and percentage layouts over computed pixel widths where you can.
- Handle safe areas (notches, home indicators, system bars) with
react-native-safe-area-contextso content is not drawn under them. This matters more now that Android apps draw edge to edge.
Do
- Use
useWindowDimensions()for size-dependent layout - Wrap screens with safe area insets
Avoid
const { width } = Dimensions.get("window")at the top of a file