skip to content

Core UI Primitives

The built-in components every screen is built from: View and Text, TextInput, Image, Pressable, Modal and ScrollView. Interviewers start here to see whether you think in native views or in the DOM.

part ofReact Nativeoverview, primer and where to startread it →
on this pageshow

explore

questions

page 1 of 2

In React Native, why does an Image with a require() source appear without a size while a remote uri Image renders blank?

level: juniorimportance: must knowfreq 72%

answer

  1. one size is known at build time
  2. the bundler records width and height
  3. remote images arrive with no size
  4. no auto-size by design: no layout jumps
  5. width and height, or aspectRatio

basics

~20 s

A require() source is a bundled asset whose width and height the bundler records, so Image sizes itself on mount. A remote uri has no known size, and React Native deliberately never sizes it after download, so without dimensions it lays out at zero.

solid answer

~50 s

`require('./icon.png')` returns an opaque asset reference, a number, and the asset's metadata includes its **width and height**, so `Image` has a default size the moment it mounts. A remote source is `{uri: 'https://…'}`: nothing is known until the download finishes, and React Native **intentionally does not** resize the view afterwards, because images popping to their natural size would make the layout jump. With no `width` and `height` in the style, the image lays out with zero size and appears blank. Give remote images explicit dimensions, or a width plus an `aspectRatio` known from your API. The same applies to `data:` URIs and to images referenced by name from the native app's resources. To let a bundled image stretch with flex instead of its intrinsic size, the docs suggest `{width: undefined, height: undefined}` in its style.

code

tsx · 18 lines
tsx
import {Image, StyleSheet, View} from 'react-native';

type Props = {photoUrl: string; ratio: number};

export function ListingHero({photoUrl, ratio}: Props) {
  return (
    <View>
      {/* Remote: no size is known until download, so give it one. */}
      <Image source={{uri: photoUrl}} style={[styles.hero, {aspectRatio: ratio}]} />
      {/* Bundled: the asset carries its own width and height. */}
      <Image source={require('./icons/bed.png')} />
    </View>
  );
}

const styles = StyleSheet.create({
  hero: {width: '100%'},
});

go deeper

for a junior

Recall that bundled require() images know their size while remote uri images need explicit width and height, or they render blank.

for a middle

Explain why React Native refuses to auto-size remote images, how aspectRatio and source dimensions help, and why data: URIs and native resource names need sizes too.

for a senior

Design gallery layouts that never wait on image downloads, by getting aspect ratios from the API and reserving getSize for rare cases.

for a principal

Push for image dimensions or ratios in the content API contract so every client can lay out stably, instead of each screen measuring at runtime.

## Two kinds of source `Image` takes a `source` prop, and the two common forms behave differently at layout time. | | Bundled: `require('./bed.png')` | Remote: `{uri: 'https://…/photo.jpg'}` | |---|---|---| | What the value is | an opaque asset reference (a number) | an object with a URL | | Size known at mount | yes, recorded when the app is bundled | no, only after download | | Default layout | the asset's own width and height | zero width and height | | Needs explicit size | no (optional) | **yes** | | Density variants | `@2x`/`@3x` picked automatically | whatever the server returns | `Image.resolveAssetSource()` shows what a bundled reference resolves to: an object with `uri`, `width`, `height` and `scale`. ## Why React Native does not auto-size remote images A browser renders an unsized image as an empty box, downloads it, then resizes the box to the image's natural size, pushing everything below it down. React Native **deliberately does not implement this**. Knowing the dimensions or aspect ratio up front is more work, but the layout is stable: nothing moves when the photo arrives. The consequence is the classic bug: `<Image source={{uri}} />` with no size is laid out at **0 x 0**, the download may even succeed, and the user sees nothing. ## Sizing remote images In a real-estate listing gallery, bundled icons (bedroom, bathroom, parking) sit next to remote photos of the property. The photos need a size from somewhere: 1. **Fixed size**: `style={{width: 120, height: 90}}` for thumbnails in a grid. 2. **Width plus aspect ratio**: `style={{width: '100%', aspectRatio: 4 / 3}}` for a full-width hero, with the ratio taken from the listing API when it provides one. 3. **Measured at runtime**: `Image.getSize(url)` resolves to the image's pixel width and height, but it may download the image first, so use it sparingly, not for every thumbnail. 4. **From the source object**: the `source` may carry `width` and `height` if known in advance, which set the default dimensions of the `Image`. `resizeMode` then decides how the photo fits the box you chose. ## Sources that also need a size The explicit-size rule is not only for HTTP URLs: - **`data:` URIs** (base64 images from an API) need width and height; the docs recommend them only for very small, dynamic images. - **Images referenced by name** from the native app's resources, such as `{uri: 'app_icon'}` for an iOS asset catalog or an Android drawable, need manual dimensions and have no build-time safety check. - **Android XML drawables** need explicit dimensions too. ## When a bundled image should not use its own size Because a `require` source brings its intrinsic size, it can fight flex layout: an icon meant to fill a flexible container keeps its natural dimensions. The docs note that you may need `{width: undefined, height: undefined}` on the style to let flex size it instead. ## Debugging a blank remote image When a remote photo does not appear, check in this order: 1. **Does the `Image` have a size?** Inspect its layout; a 0 x 0 frame is the most common cause. 2. **Did the load fail?** Add `onError` and log `nativeEvent.error`: a 404, an expired signed URL or a blocked plain-HTTP request all look like an empty box. 3. **Is the frame hidden?** An `Image` inside a parent with no height, or clipped by `overflow: 'hidden'`, can load successfully and still be invisible. 4. **Is the format supported?** The docs note, for example, that `webp` on iOS is supported only when bundled with the JavaScript code. ## Other network source options The object form also accepts `method`, `headers` and `body` for the image request, useful for authenticated listing photos, and on iOS a `cache` policy. The docs strongly recommend HTTPS, which satisfies iOS App Transport Security requirements.

  • In React Native, why does Image.getSize make a poor way to size every thumbnail in a listing grid?
    `Image.getSize(url)` resolves to the image's pixel dimensions, but to learn them it may have to download the image first. Calling it for every thumbnail doubles the work before anything can be laid out, and the grid still waits on the network. Fixed thumbnail sizes, or an aspect ratio delivered by the listing API, give a stable layout without any download.
  • In React Native, how do you make a bundled require() image fill a flexible container instead of keeping its own size?
    A `require` source carries the asset's intrinsic width and height, which become the `Image`'s default size. The docs suggest setting `{width: undefined, height: undefined}` in the style so flex layout can size the image instead, then using `resizeMode` to control how it fits.

saying these in an interview costs you the question

  • A remote Image resizes itself to the photo once the download completes.
  • A remote Image renders blank because the URL must use a require() call.
  • require() returns the file path as a string.
  • Only HTTP images need explicit dimensions; data: URIs size themselves.
  • Setting resizeMode fixes a remote Image that has no width or height.
open as a page

In React Native, what do Image's resizeMode values do, which is the default, and which would you use for listing photos versus a floor plan?

level: juniorimportance: must knowfreq 58%

basics

~20 s

resizeMode sets how an Image fits a box of a different shape: cover (the default) fills and crops, contain shows everything with bands, stretch distorts, repeat tiles, center never enlarges. Listing photos suit cover; a floor plan needs contain.

open as a page

In React Native 0.87, why is core SafeAreaView deprecated, and how do you keep a chat screen clear of the notch using react-native-safe-area-context?

level: juniorimportance: must knowfreq 58%

basics

~10 s

Core SafeAreaView is iOS-only, applies padding only and does not work with Android edge-to-edge, so 0.81 deprecated it. Use react-native-safe-area-context: SafeAreaProvider at the root, its SafeAreaView with edges, or useSafeAreaInsets for explicit padding.

open as a page

Why does a React Native Switch snap back after the user toggles it, and how do value and onValueChange fix it?

level: juniorimportance: must knowfreq 45%

basics

~20 s

React Native's Switch is controlled: after the native toggle moves, the component resets it to the value prop. If onValueChange does not update the state behind value, the switch snaps back, so store the new boolean it receives.

open as a page

In React Native, why do teams build custom buttons with Pressable instead of the core Button or the Touchable* components?

level: juniorimportance: must knowfreq 66%

basics

~20 s

Core Button renders a fixed platform look from a string title and has no style prop, and each Touchable bakes in one feedback effect. Pressable exposes the pressed state and the full press lifecycle, so any design can be built on it.

open as a page

In React Native, when is a ScrollView the right container, and when should a screen switch to a FlatList instead?

level: juniorimportance: must knowfreq 82%

basics

~20 s

A React Native ScrollView renders all its children at once, so it suits short, bounded content like a terms page or a settings form. Long or data-driven lists belong in a FlatList, which renders rows lazily and drops far ones.

open as a page

In React Native, how do a TextInput's onChangeText and onChange callbacks differ, and when would you pass defaultValue instead of value?

level: juniorimportance: must knowfreq 70%

basics

~20 s

onChangeText receives the new text as a plain string; onChange receives the native event, whose nativeEvent carries text, eventCount and target. Both fire for the same edit. defaultValue seeds an uncontrolled field; value makes the native field match your state.

open as a page

In React Native, why must raw strings sit inside a Text component instead of a View, and what happens when one does not?

level: juniorimportance: must knowfreq 78%

basics

~20 s

A React Native View is a layout box with no text drawing; only Text owns text layout. A string or number placed directly in a View is not drawn, and development builds report 'Text strings must be rendered within a <Text> component.'

open as a page

In React Native, how does KeyboardAvoidingView keep a chat composer above the keyboard, and what do behavior and keyboardVerticalOffset do?

level: middleimportance: must knowfreq 62%

basics

~20 s

KeyboardAvoidingView measures its frame, listens for keyboard events and shifts its content by the overlap, using behavior padding, height or position. Without behavior it does nothing. keyboardVerticalOffset adds the distance from the top of the screen to the view, such as a header.

open as a page

In React Native, what happens when the Android back button is pressed while a Modal is open, and why does onRequestClose matter?

level: middleimportance: must knowfreq 52%

basics

~20 s

Android's back press never closes a React Native Modal by itself: the modal's native dialog intercepts it and calls onRequestClose, whose handler must set visible to false. Without the handler, back does nothing, and BackHandler listeners do not fire while the modal is open.

open as a page

In a React Native Pressable, in what order do onPressIn, onPressOut, onPress and onLongPress fire, and when is onPress skipped?

level: middleimportance: must knowfreq 50%

basics

~20 s

A tap fires onPressIn, onPressOut, then onPress. Holding past delayLongPress (500 ms by default) fires onLongPress while the finger is down; if onLongPress is set, the release gives onPressOut but no onPress. Sliding off or a parent scroll also skips onPress.

open as a page

In a React Native checkout form, how do you move focus to the next TextInput when the user presses the keyboard's return key?

level: middleimportance: must knowfreq 58%

basics

~10 s

Give each field returnKeyType="next" (the last "done"), and in onSubmitEditing call the next field's ref.current?.focus(). Set submitBehavior="submit" on the chained fields so the keyboard stays up between them instead of blurring and reopening.

open as a page

In a React Native poetry app, how do you render one paragraph with inline bold and tappable linked words, and what do nested pieces inherit?

level: middleimportance: must knowfreq 62%

basics

~20 s

Nest Text inside Text: React Native flattens the tree into one attributed string, so bold or linked words are inline spans that wrap with the line. Nested Text inherits its ancestor Text's text styles and can override them; a View passes none down.

open as a page

Why does a React Native quick-reply chip inside a ScrollView need two taps while the keyboard is open, and how does keyboardShouldPersistTaps fix it?

level: juniorimportance: should knowfreq 45%

basics

~20 s

With the default keyboardShouldPersistTaps 'never', a ScrollView claims the first tap while the keyboard is up, dismisses the keyboard, and the child never sees it. Setting 'handled' on every ancestor ScrollView lets tapped children respond at once.

open as a page

In React Native, how do you show a dimmed full-screen loading overlay with Modal and ActivityIndicator while a photo backup runs?

level: juniorimportance: should knowfreq 42%

basics

~20 s

Render a Modal with visible bound to backup state, transparent and animationType fade, fill it with a translucent full-size View, and centre an ActivityIndicator size large. transparent only removes the background, so the dim is your own View.

open as a page

In a React Native ScrollView, what is the difference between the style prop and the contentContainerStyle prop?

level: juniorimportance: should knowfreq 55%

basics

~20 s

In a React Native ScrollView, style sets the outer scroll viewport, while contentContainerStyle sets the inner view that wraps every child and defines the scrollable area. Child layout such as alignItems, justifyContent and padding belongs on contentContainerStyle.

open as a page

In a React Native checkout form, which keyboardType would you give the postcode and phone fields, and how do iOS and Android differ?

level: juniorimportance: should knowfreq 50%

basics

~20 s

Use phone-pad for the phone, and number-pad for a digits-only postcode such as a US ZIP, but the default keyboard with autoCapitalize="characters" for alphanumeric postcodes. Seven keyboardType values work on both platforms; the rest are iOS-only, plus Android's visible-password.

open as a page

In React Native, how do the Text props numberOfLines and ellipsizeMode truncate text, and what limit does Android put on ellipsizeMode?

level: juniorimportance: should knowfreq 46%

basics

~20 s

numberOfLines caps a React Native Text at N laid-out lines (0, the default, means no limit), and ellipsizeMode picks where the cut shows: head, middle, tail (default) or clip. On Android, beyond one line only tail works correctly.

open as a page

In React Native, how does Image pick between icon.png, [email protected] and [email protected], and why must the require() path be static?

level: middleimportance: should knowfreq 44%

basics

~20 s

Require the base name once; the device's screen density picks the @2x or @3x variant, falling back to the closest one. The path must be a literal string because the bundler resolves and packages assets at bundle time, not at runtime.

open as a page

In a React Native listing gallery, how do you show a placeholder while a remote Image loads and a fallback when it fails?

level: middleimportance: should knowfreq 42%

basics

~20 s

Use defaultSource for a bundled placeholder during loading, and onError to swap to a bundled fallback when the download fails. onLoadStart, onLoad and onLoadEnd track progress. On Android, defaultSource is ignored in debug builds, so test the placeholder in a release build.

open as a page

In React Native, how do you react to the keyboard with the Keyboard module, and which of its events are available on Android versus iOS?

level: middleimportance: should knowfreq 36%

basics

~20 s

Keyboard.addListener subscribes to keyboard events and returns a subscription you remove() on cleanup. iOS emits will- and did- show, hide and change-frame events; Android only keyboardDidShow and keyboardDidHide. Keyboard.dismiss() hides the keyboard and blurs the input.

open as a page

In React Native, how do Alert.alert buttons for a confirm-delete prompt behave differently on iOS and Android?

level: middleimportance: should knowfreq 44%

basics

~20 s

Alert.alert shows a native dialog and returns nothing; each button's onPress delivers the choice. iOS allows any number of buttons with cancel and destructive styles. Android keeps the first three, maps them from the end to positive, negative and neutral, and ignores style.

open as a page

In React Native 0.87, what can the StatusBar component still control, and why were its backgroundColor and translucent props removed?

level: middleimportance: should knowfreq 30%

basics

~20 s

In React Native 0.87, StatusBar controls barStyle (icon colour), hidden, animated and the iOS showHideTransition. backgroundColor, translucent and networkActivityIndicatorVisible were removed because Android now draws apps edge to edge, so a status bar colour has no effect.

open as a page

In React Native's Pressable, how do hitSlop and pressRetentionOffset differ, and what are their defaults?

level: middleimportance: should knowfreq 36%

basics

~20 s

hitSlop widens where a press can start and has no default. pressRetentionOffset, default top/left/right 20 and bottom 30, sets how far beyond that area an active press may drift before onPressOut fires and the tap is lost.

open as a page

How do you add pull-to-refresh to a React Native ScrollView with RefreshControl, and why must refreshing be state you set?

level: middleimportance: should knowfreq 55%

basics

~20 s

Pass a RefreshControl element to the ScrollView's refreshControl prop with refreshing and onRefresh. refreshing is a controlled prop: set it to true when onRefresh starts and back to false when the reload ends, or the spinner stops immediately.

open as a page

How do you programmatically scroll a React Native ScrollView to a given section, such as one clause on a terms-of-service page?

level: middleimportance: should knowfreq 40%

basics

~20 s

Keep a ref to the React Native ScrollView and call scrollTo({x, y, animated}) or scrollToEnd({animated}) on it. Take a section's y offset from its onLayout event when it is a direct child; animated defaults to true.

open as a page

In React Native, how do autoComplete, textContentType and secureTextEntry tell the OS what a TextInput holds, for address and password autofill?

level: middleimportance: should knowfreq 40%

basics

~20 s

autoComplete is the cross-platform autofill hint (street-address, postal-code, tel, new-password); on iOS React Native translates it into textContentType, the iOS-only prop, which wins if both are set. secureTextEntry masks the characters; it stores nothing and does not work with multiline.

open as a page

In a React Native grocery checkout, what changes when the delivery-instructions TextInput is multiline, and what differs between iOS and Android?

level: middleimportance: should knowfreq 38%

basics

~20 s

With multiline, return inserts a newline instead of submitting (submitBehavior defaults to 'newline'), the field grows with its content unless a height bounds it, and text sits at the top on iOS but centred on Android until you set textAlignVertical: 'top'.

open as a page

In React Native, what does a View's onLayout callback report, when does it fire, and what goes wrong when you copy its result into state?

level: middleimportance: should knowfreq 48%

basics

~20 s

onLayout delivers nativeEvent.layout (x, y, width, height, with x and y relative to the parent) on mount and whenever the layout changes. It arrives after layout, so copying it into state costs a second render and a visible jump.

open as a page

In React Native, what do the four View pointerEvents values do, and which lets taps pass through an overlay while its buttons work?

level: middleimportance: should knowfreq 30%

basics

~20 s

pointerEvents sets whether a React Native View and its children can be touch targets: 'auto' (both), 'none' (neither), 'box-none' (children only) and 'box-only' (the view only). An overlay uses 'box-none' so empty areas pass taps through.

open as a page

showing 1–30 of 39