@cyberia-studio/react-hooks
v1.0.4
Published
React hooks collection
Downloads
6
Readme
React Hooks
Usage
import {
useCallbackState,
useEvents,
useObjectCallback,
useObjectEffect,
useObjectMemo,
useOutside,
usePromiseState,
useRefEffect,
useRefReady,
useTrustedPromise,
useTrustedState,
} from '@cyberia-studio/react-hooks';
useState
useTrustedState
const [state, setState, isTrusted] = useTrustedState('defaultValue', isTrustedDefault);
const [state, setState, isTrusted] = useTrustedState();
useCallbackState
const [state, setState] = useCallbackState('defaultValue');
const changeCallback = (value: string) => console.log(value);
const changeState = () => setState('newValue', changeCallback);
usePromiseState
const [state, setState] = usePromiseState<User>();
const changeState = () =>
setState({ username: 'fipnooone', website: 'https://fipnoo.one' }).then((value) => {
console.log(value);
});
useTrustedPromise
const [state, setState] = useTrustedPromise<number>();
const changeState = () =>
setState(undefined).then(() => {
// ...
});
useObject
useObjectEffect / useObjectCallback / useObjectMemo
const [users, setUsers] = useState<User[]>([]);
useObjectEffect(() => {
// ...
}, [users]);
useCallback
useOutside
useOutside(myRef, () => close(), [dep]);
useEvents
useEvents(
'click',
(event) => {
if (!isOpen) // ...
},
document,
[isOpen]
);
useRef
useRefEffect
useRefEffect((T) => {}, ref<T>, [dep]);
useRefReady
const myRef = useRefReady((T) => {});
return <div ref={myRef.set}></div>;