Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-stately
Advanced tools
react-stately is a library that provides state management utilities for building accessible and robust React components. It is part of the React Spectrum collection of libraries developed by Adobe, which aims to provide a comprehensive set of tools for building adaptive, accessible, and robust user interfaces.
useListState
The useListState hook manages the state for a list of items, including selection and focus management. This example demonstrates how to initialize a list with some items and manage their state.
import { useListState } from 'react-stately';
function MyListComponent(props) {
let state = useListState({
initialSelectedKeys: ['item1'],
items: [
{ key: 'item1', name: 'Item 1' },
{ key: 'item2', name: 'Item 2' },
{ key: 'item3', name: 'Item 3' }
]
});
return (
<ul>
{state.collection.map(item => (
<li key={item.key}>{item.name}</li>
))}
</ul>
);
}
useToggleState
The useToggleState hook manages the state for a toggleable component, such as a checkbox or switch. This example shows how to create a button that toggles between 'Selected' and 'Not Selected' states.
import { useToggleState } from 'react-stately';
function MyToggleComponent() {
let state = useToggleState();
return (
<button onClick={state.toggle}>
{state.isSelected ? 'Selected' : 'Not Selected'}
</button>
);
}
useComboBoxState
The useComboBoxState hook manages the state for a combo box component, including input value and item selection. This example demonstrates how to create a combo box with a list of items.
import { useComboBoxState } from 'react-stately';
function MyComboBoxComponent(props) {
let state = useComboBoxState({
items: [
{ key: 'item1', name: 'Item 1' },
{ key: 'item2', name: 'Item 2' },
{ key: 'item3', name: 'Item 3' }
]
});
return (
<div>
<input
value={state.inputValue}
onChange={e => state.setInputValue(e.target.value)}
/>
<ul>
{state.collection.map(item => (
<li key={item.key}>{item.name}</li>
))}
</ul>
</div>
);
}
react-use is a collection of essential React hooks that cover a wide range of use cases, from state management to side effects. It provides hooks like useList, useToggle, and useInput, which offer similar functionalities to react-stately's useListState, useToggleState, and useComboBoxState, respectively. However, react-use is more general-purpose and not specifically focused on accessibility.
zustand is a small, fast, and scalable state management library for React. It provides a simple API for managing global state and can be used to create custom hooks for specific state management needs. While zustand is not specifically designed for accessibility, it offers a flexible and performant alternative to react-stately for managing component state.
react-spring is a spring-physics-based animation library for React applications. It provides hooks for managing animated states, which can be used to create interactive and dynamic user interfaces. While react-spring is primarily focused on animations, it can complement react-stately by providing animated state transitions for components.
A library of React Hooks that provides cross-platform state management for your design system.
The easiest way to start building a component library with React Stately is by following our getting started guide. It walks through all of the steps needed to install the hooks from npm, and create your first component.
Here is a very basic example of using React Aria.
import {useRadioGroupState} from '@react-stately/radio';
function RadioGroup(props) {
let state = useRadioGroupState(props);
return (
<>
<label>
<input
type="radio"
name={state.name}
checked={state.selectedValue === 'dogs'}
onChange={() => state.setSelectedValue('dogs')}
/>
Dogs
</label>
<label>
<input
type="radio"
name={state.name}
checked={state.selectedValue === 'cats'}
onChange={() => state.setSelectedValue('cats')}
/>
Cats
</label>
</>
);
}
<RadioGroup
defaultValue="dogs"
onChange={(value) => alert(`Selected ${value}`)}
/>
React Stately is part of a family of libraries that help you build adaptive, accessible, and robust user experiences. Learn more about React Spectrum and React Aria on our website.
FAQs
Spectrum UI components in React
The npm package react-stately receives a total of 678,492 weekly downloads. As such, react-stately popularity was classified as popular.
We found that react-stately demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.