Continuous animations change a view on every frame, and sending each change through state means rendering and reconciling the component hierarchy every time. setNativeProps writes props straight to a host component's native view with no re-render of its subtree. The value then lives outside React, where it can collide with props your render function also sets, so the speed comes at a cost you should weigh.

Key terms

  • setNativeProps: a host component method that sets props directly on the native view, the React Native counterpart of setting properties on a DOM node.
  • Direct manipulation: changing a component imperatively, without using state or props to trigger a re-render of its subtree.
  • TextInputInstance: the ref type for TextInput, an element node with the extra methods clear(), isFocused(), getNativeRef() and setSelection(start, end).

Skipping the render

setNativeProps changes a component without a state or props re-render of its subtree. Use it when frequent re-rendering creates a performance bottleneck, typically a continuous animation, where rendering the component hierarchy and reconciling many views on every frame would cost too much. Outside that case it stays a rare tool. It is imperative and stores state in the native layer rather than in your components, which makes code harder to reason about.

Try setState first, with shouldComponentUpdate to skip reconciling subtrees that didn't change. That alone can make the update fast enough that you never need setNativeProps.

The method lives on element nodes, so it works only on host components. Composite components aren't backed by a native view, which is why the ref has to reach a View inside them. Style values go in a style object:

tsx
badgeRef.current?.setNativeProps({style: {opacity: 0.2}});

In development, React Native logs an error when a style key arrives as a top-level prop and the view has no top-level attribute of that name. The payload is also filtered. Only props that are valid native attributes of the view are kept, and they go to the native node as one update. If the element has no native node, or nothing survives the filter, nothing is sent.

Props the render function also sets

Props sent with setNativeProps go straight to native and don't take part in later diffs. The MutedBadge example below the lesson shows what follows from that. Its badge gets its opacity from render:

tsx
<View ref={badgeRef} style={{opacity: isMuted ? 0.5 : 1}}>

Tapping Flash sets the native opacity to 0.2. A later render that leaves opacity unchanged doesn't touch it, so the 0.2 stays active. Tapping Toggle mute flips isMuted, the render changes opacity from 1 to 0.5, and the 0.2 you set natively is ignored and overridden.

When the render function manages a prop, any re-render that changes it wins over setNativeProps. Bugs from this look random, because the native value disappears only on the renders that happen to change that prop. Give each prop one owner: either render sets it, or setNativeProps does.

Editing a TextInput

Editing the value of a TextInput is a very common use of setNativeProps. text is a valid native attribute of TextInput on both iOS and Android, so setNativeProps({text}) gets through the filter. The AddressField example fills the field from a button:

tsx
inputRef.current?.setNativeProps({text: SAVED_ADDRESS});

Typing the ref as TextInputInstance keeps the input's own methods visible to TypeScript. To empty the field, call clear(). It doesn't call setNativeProps: it sends a native command of its own with an empty string and the selection at 0.

The ownership rule applies here as well. If your render function also passes the text through a prop, the next render that changes that prop overrides the text you set natively.

Code Examples

tsx
import {useRef, useState} from 'react';
import {Button, Text, View} from 'react-native';
import type {ViewInstance} from 'react-native';

export function MutedBadge() {
  const badgeRef = useRef<ViewInstance>(null);
  const [isMuted, setIsMuted] = useState(false);

  const flash = () => {
    // Sent to the native view without re-rendering MutedBadge
    badgeRef.current?.setNativeProps({style: {opacity: 0.2}});
  };

  return (
    <View>
      <View ref={badgeRef} style={{opacity: isMuted ? 0.5 : 1}}>
        <Text>3 unread messages</Text>
      </View>
      <Button title="Flash" onPress={flash} />
      <Button title="Toggle mute" onPress={() => setIsMuted(muted => !muted)} />
    </View>
  );
}
tsx
import {useCallback, useRef} from 'react';
import {Button, TextInput, View} from 'react-native';
import type {TextInputInstance} from 'react-native';

const SAVED_ADDRESS = '14 Rue des Lilas, Lyon';

export function AddressField() {
  const inputRef = useRef<TextInputInstance>(null);

  const fillSavedAddress = useCallback(() => {
    inputRef.current?.setNativeProps({text: SAVED_ADDRESS});
  }, []);

  const clearAddress = useCallback(() => {
    inputRef.current?.clear();
  }, []);

  return (
    <View>
      <TextInput ref={inputRef} placeholder="Delivery address" />
      <Button title="Use saved address" onPress={fillSavedAddress} />
      <Button title="Clear" onPress={clearAddress} />
    </View>
  );
}
✓ Completed