Skip to content

Commit

Permalink
Fix use update
Browse files Browse the repository at this point in the history
  • Loading branch information
imbhargav5 committed Nov 18, 2020
1 parent aae4a66 commit 7717eed
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/shared/useDidUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect } from "react";
import { useDidMount } from "./useDidMount";
import { useEffect, useRef } from "react";

/**
* useDidUpdate hook
Expand All @@ -13,20 +12,19 @@ import { useDidMount } from "./useDidMount";
* @return {undefined}
*/
function useDidUpdate(callback: () => any, conditions?: Array<any>): void {
const [hasMounted, setHasMounted] = useState(false);
const hasMountedRef = useRef(false);
if (typeof conditions !== "undefined" && !Array.isArray(conditions)) {
conditions = [conditions];
} else if (Array.isArray(conditions) && conditions.length === 0) {
console.warn(
"Using [] as the second argument makes useDidUpdate a noop. The second argument should either be `undefined` or an array of length greater than 0."
);
}
useDidMount(() => {
setHasMounted(true);
});
useEffect(() => {
if (hasMounted) {
if (hasMountedRef.current) {
callback();
}else{
hasMountedRef.current = true
}
}, conditions);
}
Expand Down

0 comments on commit 7717eed

Please sign in to comment.