Placing a coach mark next to a button, or checking where a field sits inside its form, needs the view's numbers after layout. Host component refs carry three measuring methods, measure(), measureInWindow() and measureLayout(), and View has the onLayout prop. Each reports a different coordinate space, and where you call them decides whether you can act on the result in the same frame.

Key terms

  • measure(): a host component method that passes the view's x and y (its origin relative to its parent), its width and height in the viewport, and pageX and pageY (its position in the viewport) to a callback.
  • measureInWindow(): a host component method that passes the view's x, y, width and height in the window to a callback.
  • measureLayout(): a host component method that measures a view relative to the origin of an ancestor view you pass as a ref.
  • onLayout: a View prop called on mount and whenever the layout changes, with the new layout in nativeEvent.layout.

Measure inside useLayoutEffect

Call the measuring methods from a useLayoutEffect hook. There you get the most recent values, and you can apply changes in the same frame the measurements are computed. The TourStep example below the lesson puts a ref on its wrapper and measures it inside the hook:

tsx
stepRef.current?.measure((x, y, width, height, pageX, pageY) => {
  onMeasure({pageX, pageY, width, height});
});

The ref has to reach a host component. The methods exist on most built-in components but not on composite components that aren't backed by a native view, so a ref on one of your own components needs the pass-through covered in the previous lesson.

Measurements exist only after rendering has completed in native. measure() also checks that the element still has a native node first: on an unmounted view the callback is never called, and nothing is thrown. Code that waits for that callback can't assume it will run.

Viewport or window coordinates

measure(callback) calls back with six numbers in this order: x, y, width, height, pageX, pageY. x and y are the view's origin relative to its parent. The docs describe them as a position in the viewport, but the native code that builds the result fills them from the view's frame inside its parent. For screen coordinates, read pageX and pageY: they are the view's coordinates in the viewport, which is typically the whole screen. The width and height are the view's size in the viewport. For the component's actual size, read it from onLayout.

measureInWindow(callback) calls back with four numbers, x, y, width, height, with no pageX or pageY. Its x and y are the view's location in the window. When your React root view is embedded in another native view, these are the absolute coordinates, which makes measureInWindow() the method for that setup.

Both methods pass their results to a callback instead of returning them, while getBoundingClientRect() returns the rectangle directly.

Coordinates relative to an ancestor

measureLayout(relativeToNativeComponentRef, onSuccess, onFail) works like measure(), but the coordinates are relative to the origin of an ancestor view passed as the first argument. The success callback gets left, top, width, height. onFail is optional. Native calls it when the measurement can't be computed, for example when either view is no longer in the current tree.

The FieldOffset example measures an input against the form around it. The first parameter of measureLayout() is typed number | HostInstance and does not accept null, so the effect checks formRef.current before the call:

tsx
fieldRef.current?.measureLayout(form, (left, top, width, height) => {

The first argument must be a native element ref. The docs still describe passing a numeric node handle, a variant they call obsolete with the New Architecture. Code that follows them gets nothing back: given anything other than a native element ref, measureLayout() doesn't measure, logs ref.measureLayout must be called with a ref to a native component. in development, and calls neither callback.

When onLayout is the better tool

onLayout is called on mount and whenever the layout changes. The event carries nativeEvent.layout with x, y, width and height, where x and y are coordinates inside the parent component, not on screen. It fires as soon as the layout is calculated, so the new layout may not be on screen yet when the event arrives, especially while a layout animation is running.

Pick onLayout over measure() when you need the measurements as soon as possible and don't need pageX and pageY. Reach for a measuring method in useLayoutEffect when you need viewport, window or ancestor coordinates, or want to apply the result in the same frame.

Code Examples

tsx
import {useLayoutEffect, useRef} from 'react';
import type {ReactNode} from 'react';
import {View} from 'react-native';
import type {ViewInstance} from 'react-native';

type StepRect = {pageX: number; pageY: number; width: number; height: number};

type TourStepProps = {
  onMeasure: (rect: StepRect) => void;
  children: ReactNode;
};

export function TourStep({onMeasure, children}: TourStepProps) {
  const stepRef = useRef<ViewInstance>(null);

  useLayoutEffect(() => {
    stepRef.current?.measure((x, y, width, height, pageX, pageY) => {
      onMeasure({pageX, pageY, width, height});
    });
  }, [onMeasure]);

  return <View ref={stepRef}>{children}</View>;
}
tsx
import {useLayoutEffect, useRef, useState} from 'react';
import {Text, TextInput, View} from 'react-native';
import type {ViewInstance} from 'react-native';

type Offset = {left: number; top: number; width: number; height: number};

export function FieldOffset() {
  const formRef = useRef<ViewInstance>(null);
  const fieldRef = useRef<ViewInstance>(null);
  const [offset, setOffset] = useState<Offset | null>(null);

  useLayoutEffect(() => {
    const form = formRef.current;
    if (form == null) {
      return;
    }
    // Coordinates are relative to the form's origin
    fieldRef.current?.measureLayout(form, (left, top, width, height) => {
      setOffset({left, top, width, height});
    });
  }, []);

  return (
    <View ref={formRef} style={{padding: 24}}>
      <Text>Email</Text>
      <View ref={fieldRef}>
        <TextInput placeholder="you@example.com" />
      </View>
      {offset && <Text>{`Field starts ${offset.top} below the form's top edge`}</Text>}
    </View>
  );
}
✓ Completed