shallow.ts
This file
Path
src/react/shallow.ts
12 lines · 354 bytes · TypeScript
Last changed by
Not known.
History of this fileTake it
12345678910111213import React from 'react'import { shallow } from '../vanilla/shallow.ts'
export function useShallow<S, U>(selector: (state: S) => U): (state: S) => U { const prev = React.useRef<U>(undefined) return (state) => { const next = selector(state) return shallow(prev.current, next) ? (prev.current as U) : (prev.current = next) }}