Skip to content

Commit

Permalink
chore: Update useFormattedCode to useShallowMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jul 8, 2020
1 parent 321acbf commit 0c6ca59
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/hooks/useFormattedCode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMemo } from 'react';
import useShallowMemo from './useShallowMemo';
import formatCode from '../utils/formatCode';

const useFormattedCode = (code, options) => {
return useMemo(() => {
return useShallowMemo(() => {
try {
return formatCode(code, options);
} catch (e) {
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useShallowMemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useMemo, useRef } from 'react';
import shallowEqual from '../utils/shallowEqual';

const useShallowMemo = (callback, deps) => {
const depsRef = useRef([]);
const previous = depsRef.current;
const equal = deps.every((dep, idx) => shallowEqual(dep, previous[idx]));

if (!equal) {
depsRef.current = deps;
}

// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(callback, depsRef.current);
};

export default useShallowMemo;

0 comments on commit 0c6ca59

Please sign in to comment.