mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
* test fuma toc * make toc work * ok toc * ui-patterns toc * fix types * cleanup * fix overflow issues * cleanup * fix loading w * Update apps/docs/components/GuidesTableOfContents.tsx * fix prettier * format * install deps * target ES2018 * tweak regex * update docs test snapshots * fix useSubscribeTocRerender
28 lines
684 B
TypeScript
28 lines
684 B
TypeScript
import { useState } from 'react'
|
|
|
|
function isDifferent(a: unknown, b: unknown): boolean {
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
return b.length !== a.length || a.some((v, i) => isDifferent(v, b[i]))
|
|
}
|
|
|
|
return a !== b
|
|
}
|
|
|
|
/**
|
|
* @param value - state to watch
|
|
* @param onChange - when the state changed
|
|
* @param isUpdated - a function that determines if the state is updated
|
|
*/
|
|
export function useOnChange<T>(
|
|
value: T,
|
|
onChange: (current: T, previous: T) => void,
|
|
isUpdated: (prev: T, current: T) => boolean = isDifferent
|
|
): void {
|
|
const [prev, setPrev] = useState<T>(value)
|
|
|
|
if (isUpdated(prev, value)) {
|
|
onChange(value, prev)
|
|
setPrev(value)
|
|
}
|
|
}
|