useObjectRef
Offers an object ref for a given callback ref or an object ref. Especially helfpul when passing forwarded refs (created using React.forwardRef) to React Aria Hooks.
| install | yarn add @react-aria/utils |
|---|---|
| version | 3.8.2 |
| usage | import {useObjectRef} from '@react-aria/utils' |
API#
useObjectRef<T>(
(forwardedRef: (
(instance: T
| | null
)) => void
| MutableRefObject<T
| | null>
| null
)): MutableRefObject<T>Introduction#
The useObjectRef hook creates an object ref for a given ref.
The given ref may be an object ref or a callback ref.
The created ref is particularly helpful when defining a custom component using
React.forwardRef, and wanting to pass the forwarded ref to
a @react-aria Hook that accepts a ref (e.g. useButton).
Example using an object ref#
const TextField = ReactforwardRef((props forwardedRef) => {
const {label} = props;
const ref = useObjectRef(forwardedRef);
const {labelProps inputProps} = useTextField(props ref);
return (
<div style={display: 'flex' flexDirection: 'column' width: 200}>
<label ...labelProps>label</label>
<input ...inputProps ref=ref />
</div>
);
});
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=textFieldRef
/>
</p>
<p>
<button onClick=() => textFieldRefcurrentfocus()>
Click me to focus text field
</button>
</p>
</div>
);
}
<Example />const TextField = ReactforwardRef(
(props forwardedRef) => {
const {label} = props;
const ref = useObjectRef(forwardedRef);
const {labelProps inputProps} = useTextField(
props
ref
);
return (
<div
style={
display: 'flex'
flexDirection: 'column'
width: 200
}>
<label ...labelProps>label</label>
<input ...inputProps ref=ref />
</div>
);
}
);
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=textFieldRef
/>
</p>
<p>
<button
onClick=() => textFieldRefcurrentfocus()>
Click me to focus text field
</button>
</p>
</div>
);
}
<Example />const TextField = ReactforwardRef(
(
props
forwardedRef
) => {
const {
label
} = props;
const ref = useObjectRef(
forwardedRef
);
const {
labelProps
inputProps
} = useTextField(
props
ref
);
return (
<div
style={
display:
'flex'
flexDirection:
'column'
width: 200
}>
<label
...labelProps>
label
</label>
<input
...inputProps
ref=ref
/>
</div>
);
}
);
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=
textFieldRef
/>
</p>
<p>
<button
onClick=() =>
textFieldRefcurrentfocus()
>
Click me to
focus text
field
</button>
</p>
</div>
);
}
<Example />Example using a callback ref#
const TextField = ReactforwardRef((props forwardedRef) => {
// Same as in the previous example...
});
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=(el) => (textFieldRefcurrent = el)
/>
</p>
<p>
<button onClick=() => textFieldRefcurrentfocus()>
Click me to focus text field
</button>
</p>
</div>
);
}
<Example />const TextField = ReactforwardRef(
(props forwardedRef) => {
// Same as in the previous example...
}
);
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=(el) => (textFieldRefcurrent = el)
/>
</p>
<p>
<button
onClick=() => textFieldRefcurrentfocus()>
Click me to focus text field
</button>
</p>
</div>
);
}
<Example />const TextField = ReactforwardRef(
(
props
forwardedRef
) => {
// Same as in the previous example...
}
);
function Example() {
const textFieldRef = ReactuseRef();
return (
<div>
<p>
<TextField
label="Email"
placeholder="abc@example.com"
ref=(el) =>
(textFieldRefcurrent = el)
/>
</p>
<p>
<button
onClick=() =>
textFieldRefcurrentfocus()
>
Click me to
focus text
field
</button>
</p>
</div>
);
}
<Example />