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
| Prop | On the web |
|---|---|
style | Compiles to atomic CSS. See Styles. |
role, accessibilityRole | Picks the element. See Semantic HTML. |
accessibilityLabel and the other accessibility props | Become ARIA attributes. See Accessibility props. |
onLayout | Fires 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 props | As on native. See Responder props. |
pointerEvents | none, auto, box-none, and box-only, as on native. |
focusable, tabIndex | focusable sets tabIndex to 0, or to -1 for false. tabIndex wins when you set both. |
nativeID, id, testID | id, id, and data-testid. |
href | Web only. Renders an <a>. |
hrefAttrs | Web only. The react-native-web form of target, rel, and download. See Props from react-native-web. |
dataSet | Web only. The react-native-web form of data-* props. See Props from react-native-web. |
inert | Web only. The element and its children take no focus and no clicks, and screen readers skip them. |
suppressHydrationWarning | Web only. React doesn't report a difference from the server HTML in the text and the attributes of this element. See Server rendering. |
aria-level | Web only. The heading level of role="heading", 1 to 6. |
onClick, onKeyDown, and the other DOM handlers | Web only. Every React DOM event handler passes to the element. |
accessibilityActions, onAccessibilityAction | Accepted and dropped. See Accessibility props. |
collapsable, accessible | Accepted 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
Capturecallbacks 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,onResponderTerminationRequestof the holder decides. - A view, a
PanResponder, and aPressableshare 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 withonStartShouldSetResponder, because on native the field is a responder of its own.onStartShouldSetResponderCapturestill asks, and a view withonMoveShouldSetRespondercan take the drag, as on native. - The event has the touch fields of React Native, such as
nativeEvent.locationX,pageX,touches, andchangedTouches. - 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,onResponderTerminationRequestdecides first. - A touch scroll of the page or of a
ScrollViewtakes the gesture, and the view getsonResponderTerminate. ReturntruefromonResponderGrantto stop the scroll while the view holds the gesture, as on Android. - A view with
onMoveShouldSetResponderholds a one-finger touch until it decides, unless the page or aScrollViewcan scroll along the move. Then the browser scrolls, as a scroll view does on native.
Not as on native:
hitSlopon aViewdoesn't make the touch area bigger. Use it on aPressable.- A view with the
panHandlersof aPanResponderand responder props uses thepanHandlers, and the responder props do nothing. In development, a warning tells you to put the callbacks inPanResponder.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.
| Method | On 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.