package.dist.utils.use-controllable-state.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of react Show documentation
Show all versions of react Show documentation
A collection of unstyled, accessible UI components for React, utilizing state machines for seamless interaction.
The newest version!
'use client';
import { useState, useCallback } from 'react';
function useControllableState(props) {
const { value, onChange, defaultValue } = props;
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
const controlled = value !== void 0;
const currentValue = controlled ? value : uncontrolledValue;
const setValue = useCallback(
(value2) => {
if (controlled) {
return onChange?.(value2);
}
setUncontrolledValue(value2);
return onChange?.(value2);
},
[controlled, onChange]
);
return [currentValue, setValue];
}
export { useControllableState };
© 2015 - 2025 Weber Informatics LLC | Privacy Policy