Files
headlessui/packages/@headlessui-react/src/hooks/use-computed.ts
T
Robin Malfait ef00732685 cleanup and consistency (#213)
- Made the use of `const` and `let` consistent
- import required functions and types from 'react' instead of using the
  `React.` namespace.
- Added `Expand` type, which can expand complex types to their "final"
  result.
- Ensured that we use `as const` for DEFAULT_XXX_TAG where we used a
  string. So that we have the type of `div` instead of `string` for
  example.
- Used `interface` over `type` where possible. I'm personally more of a
  `type` fan. But the TypeScript recommends `interfaces` where possible
  because they are faster, yield better error messages and so on.
2021-01-30 14:46:54 +01:00

13 lines
421 B
TypeScript

import { useState, useRef } from 'react'
import { useIsoMorphicEffect } from './use-iso-morphic-effect'
export function useComputed<T>(cb: () => T, dependencies: React.DependencyList) {
let [value, setValue] = useState(cb)
let cbRef = useRef(cb)
useIsoMorphicEffect(() => {
cbRef.current = cb
}, [cb])
useIsoMorphicEffect(() => setValue(cbRef.current), [cbRef, setValue, ...dependencies])
return value
}