useCollator
Provides localized string collation for the current locale. Automatically updates when the locale changes, and handles caching of the collator for performance.
install | yarn add @react-aria/i18n |
---|---|
version | 3.3.4 |
usage | import {useCollator} from '@react-aria/i18n' |
Introduction#
useCollator
wraps a builtin browser Intl.Collator
object to provide a React Hook that integrates with the i18n system in React Aria. It handles string comparison according to the current locale,
updating when the locale changes, and caching of collators for performance. See the
Intl.Collator docs for
information.
API#
useCollator(
(options: Intl.CollatorOptions
)): Intl.Collator
Example#
This example includes two textfields and compares the values of the two fields using a collator according to the current locale.
function Example() {
let [first setFirst] = ReactuseState('');
let [second setSecond] = ReactuseState('');
let collator = useCollator();
let result = collatorcompare(first second);
return (
<>
<div>
<input
aria-label="First string"
placeholder="First string"
value= first
onChange=(e) => setFirst(etargetvalue)
/>
<input
aria-label="Second string"
placeholder="Second string"
value= second
onChange=(e) => setSecond(etargetvalue)
/>
</div>
<p>
result === 0
? 'The strings are the same'
: result < 0
? 'First comes before second'
: 'Second comes before first'
</p>
</>
);
}
function Example() {
let [first setFirst] = ReactuseState('');
let [second setSecond] = ReactuseState('');
let collator = useCollator();
let result = collatorcompare(first second);
return (
<>
<div>
<input
aria-label="First string"
placeholder="First string"
value= first
onChange=(e) => setFirst(etargetvalue)
/>
<input
aria-label="Second string"
placeholder="Second string"
value= second
onChange=(e) => setSecond(etargetvalue)
/>
</div>
<p>
result === 0
? 'The strings are the same'
: result < 0
? 'First comes before second'
: 'Second comes before first'
</p>
</>
);
}
function Example() {
let [
first
setFirst
] = ReactuseState('');
let [
second
setSecond
] = ReactuseState('');
let collator = useCollator();
let result = collatorcompare(
first
second
);
return (
<>
<div>
<input
aria-label="First string"
placeholder="First string"
value= first
onChange=(
e
) =>
setFirst(
etarget
value
)
/>
<input
aria-label="Second string"
placeholder="Second string"
value= second
onChange=(
e
) =>
setSecond(
etarget
value
)
/>
</div>
<p>
result === 0
? 'The strings are the same'
: result < 0
? 'First comes before second'
: 'Second comes before first'
</p>
</>
);
}