Skip to content

Commit

Permalink
added cleanup to force update hook
Browse files Browse the repository at this point in the history
  • Loading branch information
leoggonzalez committed Oct 20, 2022
1 parent 61d5f1c commit dc715f6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

export function useForceUpdate(): () => void {
const mounted = useRef(false);
const [, updateState] = useState(0);

function handleUpdate(): void {
if (mounted.current) {
updateState((state) => state + 1);
}
}

useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);

return () => {
updateState((state) => state + 1);
handleUpdate();
};
}

Expand Down

0 comments on commit dc715f6

Please sign in to comment.