React Native theorytheory 0/50 · 0%
Fundamentals · easy

18. Conditional rendering and lists

Showing UI based on state and mapping data.

Conditional rendering uses plain JS: ternaries for either/or, `&&` for show-or-nothing, and early returns for whole different screens (e.g. a loading screen).

function Screen({ loading, address }: { loading: boolean; address: string | null }) {
  if (loading) return <Text>Loading…</Text>;
  return (
    <View>
      {address ? <Text>{address}</Text> : <Text>Not connected</Text>}
      {address && <Text>Tap to disconnect</Text>}
    </View>
  );
}

Be careful with `&&` and numbers: `{count && <Text>...</Text>}` renders a literal `0` when `count` is 0, so prefer `{count > 0 && ...}`.

Check your understanding

  1. 1. What does `{0 && <Text>x</Text>}` render?

  2. 2. How do you show a completely different screen while loading?

  3. 3. What operator conditionally renders only when true?