Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State memoization with deep comparison #39

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"rimraf": "^2.6.2"
},
"peerDependencies": {
"react": "^16.8.0"
"react": "^16.8.0 || ^17.0.0"
},
"jest": {
"coverageThreshold": {
Expand All @@ -63,6 +63,7 @@
}
},
"dependencies": {
"@use-it/event-listener": "^0.1.2"
"@use-it/event-listener": "^0.1.2",
"dequal": "^2.0.2"
}
}
15 changes: 15 additions & 0 deletions src/useMemoizedObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useRef } from 'react';
import { dequal } from 'dequal/lite';

const useMemoizedObject = (obj) => {
const mem = useRef();

if (dequal(mem.current, obj)) {
return mem.current;
}

mem.current = obj;
return obj;
};

export default useMemoizedObject;
24 changes: 12 additions & 12 deletions src/usePersistedState.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { useState, useEffect, useRef } from 'react';
import { dequal } from 'dequal/lite';
import useEventListener from '@use-it/event-listener';

import createGlobalState from './createGlobalState';
import useMemoizedObject from './useMemoizedObject';

const usePersistedState = (initialState, key, { get, set }) => {
const globalState = useRef(null);
const [state, setState] = useState(() => get(key, initialState));
const [_state, setState] = useState(() => get(key, initialState));
const state = useMemoizedObject(_state);

// subscribe to `storage` change events
useEventListener('storage', ({ key: k, newValue }) => {
if (k === key) {
const newState = JSON.parse(newValue);
if (state !== newState) {
if (!dequal(state, newState)) {
setState(newState);
}
}
Expand All @@ -28,16 +31,13 @@ const usePersistedState = (initialState, key, { get, set }) => {
}, []);

// Only persist to storage if state changes.
useEffect(
() => {
// persist to localStorage
set(key, state);

// inform all of the other instances in this tab
globalState.current.emit(state);
},
[state]
);
useEffect(() => {
// persist to localStorage
set(key, state);

// inform all of the other instances in this tab
globalState.current.emit(state);
}, [state]);

return [state, setState];
};
Expand Down