Most primitive components have an asChild prop. When it is set to true, it passes all of its props to its immediate child and renders the child instead of the primitive. To make this possible, the internal implementation of the primitive uses the Slot primitive.
Examples
Implementation with an asChild prop
Using the asChild prop to conditionally render a custom Button as a Pressable or a Slot.Pressable.
Use of a component that has an asChild prop
Setting the asChild prop to true to pass all of the Button props to the Pressable component.
Forwarding Refs
Refs are used for Direct Manipulation. A common practice is to set a ref to a component, then use the ref to call methods or access properties of the component.
Passing a ref to react-native components
Passing a ref to the TextInput component, allows us to call the focus method when the Pressable is pressed.
Custom components with ref
In React, the ref prop is a reserved prop since it needs to be handled differently. To forward a ref to a child component, you must use the React.forwardRef function.
Creating a component that accepts a ref
Programmatically Showing and Hiding Components
For most components, you can use props to manipulate the visibility of it or its children components. For example, you can pass the open prop to the AlertDialog to show or hide it.
However, for some components, the position of its content depends on the onPress or onLongPress events. In this case, you will need to pass a ref to the trigger component and call a method to show or hide it.
Example with content that depends on the trigger position
A Portal Component inside a Presentation Modal screen
Import the PortalHost component
Import the FullWindowOverlay component
Set a custom name for your portal and place it in a variable at the top of the file.
Create a window overlay component that adapts based on the platform (iOS or others)
In the component where you need a portal, pass the custom name
At the bottom of your content (the fragment wrapping everything), place the WindowOverlay component. Inside it, add PortalHost as a child.
Make sure the PortalHost also gets the custom portal name by passing it to the PortalHost component.