usePressable
What Pressable does, for a component of your own.
usePressable is what Pressable is made with:
presses, long presses, and whether it is pressed, hovered or focused. Spread
its pressableProps on a view, and draw it from the state it returns.
function Button({ label, onPress }: { label: string; onPress: () => void }) {
const press = usePressable({ onPress });
return (
<View
{...press.pressableProps}
style={[
styles.button,
press.hovered && styles.hovered,
press.pressed && styles.pressed,
press.focusVisible && styles.focusRing,
]}
>
<Text>{label}</Text>
</View>
);
}- Options are
Pressable's:onPress,onLongPressanddelayLongPress,onPressInandonPressOut,onHoverInandonHoverOut, anddisabled. The latest ones are called, so they need nouseCallback. - State:
pressed,hovered,focusedandfocusVisible, asPressable's style function is given. They render when they change. pressablePropsmake the view focusable, and to assistive technology abuttonthat offersactivateand, withonLongPress,longpress. Props after the spread override them, such as anotheraccessibilityRole. Give your ownaccessibilityStateandaccessibilityActionsto the hook rather than the view, sodisabledand its actions are added to them.- A ref:
pressablePropstakes the view's ref, so pass one of your own as therefoption.