mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
406 B
TypeScript
18 lines
406 B
TypeScript
import { useLayoutEffect, useState } from 'react';
|
|
import { useEvent } from './useEvent';
|
|
|
|
export function useLazyValue<T>(value: T, onChange: (val: T) => void) {
|
|
const [inner, setInner] = useState(value);
|
|
|
|
useLayoutEffect(() => {
|
|
setInner(value);
|
|
}, [value]);
|
|
|
|
const handleChange = useEvent((val: T) => {
|
|
setInner(val);
|
|
onChange(val);
|
|
});
|
|
|
|
return [inner, handleChange] as const;
|
|
}
|