Skip to content

Commit

Permalink
Updated useDeepCompareEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
salmenus committed Mar 29, 2024
1 parent d364456 commit 17cecd8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions packages/react/core/src/hooks/useDeepCompareEffect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// * License: MIT
//

import * as React from 'react';
import {useEffect, useMemo, useRef} from 'react';
import {dequal as deepEqual} from '../dequal';

type UseEffectParams = Parameters<typeof React.useEffect>
type UseEffectParams = Parameters<typeof useEffect>
type EffectCallback = UseEffectParams[0]
type DependencyList = UseEffectParams[1]
// yes, I know it's void, but I like what this communicates about
// the intent of these functions: It's just like useEffect
type UseEffectReturn = ReturnType<typeof React.useEffect>
type UseEffectReturn = ReturnType<typeof useEffect>

function checkDeps(deps: DependencyList) {
if (!deps || !deps.length) {
Expand All @@ -37,16 +37,16 @@ function isPrimitive(val: unknown) {
* @returns a memoized version of the value as long as it remains deeply equal
*/
export function useDeepCompareMemoize<T>(value: T) {
const ref = React.useRef<T>(value)
const signalRef = React.useRef<number>(0)
const ref = useRef<T>(value)
const signalRef = useRef<number>(0)

if (!deepEqual(value, ref.current)) {
ref.current = value
signalRef.current += 1
}

// eslint-disable-next-line react-hooks/exhaustive-deps
return React.useMemo(() => ref.current, [signalRef.current])
return useMemo(() => ref.current, [signalRef.current])
}

function useDeepCompareEffect(
Expand All @@ -57,15 +57,15 @@ function useDeepCompareEffect(
checkDeps(dependencies)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
return useEffect(callback, useDeepCompareMemoize(dependencies))
}

export function useDeepCompareEffectNoCheck(
callback: EffectCallback,
dependencies: DependencyList,
): UseEffectReturn {
// eslint-disable-next-line react-hooks/exhaustive-deps
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
return useEffect(callback, useDeepCompareMemoize(dependencies))
}

export default useDeepCompareEffect;
2 changes: 1 addition & 1 deletion pipeline/npm/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ from [`@nlux/themes`](https://www.npmjs.com/package/@nlux/themes) or use the
CDN hosted version from below:

```jsx
<link rel="stylesheet" href="https://themes.nlux.ai/v1.0.4/nova.css"/>
<link rel="stylesheet" href="https://themes.nlux.ai/v1.0.5/nova.css"/>
```

This CDN is provided for demo purposes only and it's not scalable.
Expand Down
2 changes: 1 addition & 1 deletion pipeline/npm/versions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"inherit": true,
"nlux": "1.0.4",
"nlux": "1.0.5",
"peerDependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down

0 comments on commit 17cecd8

Please sign in to comment.