Skip to main content

View

The basic container. It renders a <div>, or the element that its role picks, with the React Native layout defaults.

import { StyleSheet, View } from 'react-native'

export function Toolbar({ children }) {
return (
<View role="navigation" style={styles.toolbar}>
{children}
</View>
)
}

const styles = StyleSheet.create({
toolbar: { flexDirection: 'row', gap: 8, padding: 12 },
})

Props​

PropOn the web
styleCompiles to atomic CSS. See Styles.
role, accessibilityRolePicks the element. See Semantic HTML.
accessibilityLabel and the other accessibility propsBecome ARIA attributes. See Accessibility props.
onLayoutFires after mount and after each size change, from a ResizeObserver. x and y are relative to the parent view. A move with no size change doesn't fire it.
onStartShouldSetResponder, onResponderGrant, and the other responder propsAs on native. See Responder props.
pointerEventsnone, auto, box-none, and box-only, as on native.
focusable, tabIndexfocusable sets tabIndex to 0, or to -1 for false. tabIndex wins when you set both.
nativeID, id, testIDid, id, and data-testid.
hrefWeb only. Renders an <a>.
hrefAttrsWeb only. The react-native-web form of target, rel, and download. See Props from react-native-web.
dataSetWeb only. The react-native-web form of data-* props. See Props from react-native-web.
inertWeb only. The element and its children take no focus and no clicks, and screen readers skip them.
suppressHydrationWarningWeb only. React doesn't report a difference from the server HTML in the text and the attributes of this element. See Server rendering.
aria-levelWeb only. The heading level of role="heading", 1 to 6.
onClick, onKeyDown, and the other DOM handlersWeb only. Every React DOM event handler passes to the element.
accessibilityActions, onAccessibilityActionAccepted and dropped. See Accessibility props.
collapsable, accessibleAccepted and ignored.

Any other prop that isn't a React Native prop is dropped.

Responder props​

The 12 responder props of React Native work with a mouse, a pen, or a finger:

  • The negotiation is the same as on native. The Capture callbacks ask from the parent to the child, and the others from the child to the parent. One view holds the gesture at a time. When another view asks for it, onResponderTerminationRequest of the holder decides.
  • A view, a PanResponder, and a Pressable share that one gesture. A pressable inside a responder view takes a tap first, as on native.
  • A touch that starts in a text field, such as a TextInput, doesn't ask the views above it with onStartShouldSetResponder, because on native the field is a responder of its own. onStartShouldSetResponderCapture still asks, and a view with onMoveShouldSetResponder can take the drag, as on native.
  • The event has the touch fields of React Native, such as nativeEvent.locationX, pageX, touches, and changedTouches.
  • At the grant, the view captures the pointer, so the moves come to it also outside its box.
  • As in react-native-web, the context menu, a blur of the window, and a scroll of an element around the view end the gesture, and the view gets onResponderTerminate. For the context menu and the scroll, onResponderTerminationRequest decides first.
  • A touch scroll of the page or of a ScrollView takes the gesture, and the view gets onResponderTerminate. Return true from onResponderGrant to stop the scroll while the view holds the gesture, as on Android.
  • A view with onMoveShouldSetResponder holds a one-finger touch until it decides, unless the page or a ScrollView can scroll along the move. Then the browser scrolls, as a scroll view does on native.

Not as on native:

  • hitSlop on a View doesn't make the touch area bigger. Use it on a Pressable.
  • A view with the panHandlers of a PanResponder and responder props uses the panHandlers, and the responder props do nothing. In development, a warning tells you to put the callbacks in PanResponder.create.

Ref​

The ref is the DOM element, with the measure methods of a native host instance added. Each one calls its callback at once, with CSS pixels.

MethodOn the web
measure(callback)x and y in the parent element, width, height, then pageX and pageY in the window.
measureInWindow(callback)x and y in the window, width, height. The window is the visible area, so the values change when the page scrolls.
measureLayout(ancestor, onSuccess, onFail)left and top in the ancestor element, width, height. It calls onFail when the other node isn't an ancestor that is in the document.
setNativeProps({ style })Writes the style as an inline style, so it beats the style prop until you set it again. Any other prop has no effect and logs a warning.

The sizes come from getBoundingClientRect(), so they include a transform, as measure does on native. The types are HostInstance and ViewInstance.

note

A ref typed as useRef<HTMLElement>(null) no longer fits the ref prop. Use useRef<ViewInstance>(null), as on native.