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

Use superjson to parse/stringify objects instead of built in JSON's parse/stringify #314

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as React from "react";
import superjson from 'superjson';


function isShallowEqual(object1, object2) {
const keys1 = Object.keys(object1);
Expand Down Expand Up @@ -590,7 +592,7 @@ export function useList(defaultList = []) {
}

const setLocalStorageItem = (key, value) => {
const stringifiedValue = JSON.stringify(value);
const stringifiedValue = superjson.stringify(value);
window.localStorage.setItem(key, stringifiedValue);
dispatchStorageEvent(key, stringifiedValue);
};
Expand Down Expand Up @@ -625,7 +627,7 @@ export function useLocalStorage(key, initialValue) {
const setState = React.useCallback(
(v) => {
try {
const nextState = typeof v === "function" ? v(JSON.parse(store)) : v;
const nextState = typeof v === "function" ? v(superjson.parse(store)) : v;

if (nextState === undefined || nextState === null) {
removeLocalStorageItem(key);
Expand All @@ -648,7 +650,7 @@ export function useLocalStorage(key, initialValue) {
}
}, [key, initialValue]);

return [store ? JSON.parse(store) : initialValue, setState];
return [store ? superjson.parse(store) : initialValue, setState];
}

export function useLockBodyScroll() {
Expand Down Expand Up @@ -1151,7 +1153,7 @@ export function useScript(src, options = {}) {
}

const setSessionStorageItem = (key, value) => {
const stringifiedValue = JSON.stringify(value);
const stringifiedValue = superjson.stringify(value);
window.sessionStorage.setItem(key, stringifiedValue);
dispatchStorageEvent(key, stringifiedValue);
};
Expand Down Expand Up @@ -1186,7 +1188,7 @@ export function useSessionStorage(key, initialValue) {
const setState = React.useCallback(
(v) => {
try {
const nextState = typeof v === "function" ? v(JSON.parse(store)) : v;
const nextState = typeof v === "function" ? v(superjson.parse(store)) : v;

if (nextState === undefined || nextState === null) {
removeSessionStorageItem(key);
Expand All @@ -1209,7 +1211,7 @@ export function useSessionStorage(key, initialValue) {
}
}, [key, initialValue]);

return [store ? JSON.parse(store) : initialValue, setState];
return [store ? superjson.parse(store) : initialValue, setState];
}

export function useSet(values) {
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"react": ">=18.0.0",
"react-dom": ">=18.0.0"
},
"dependencies": {
"superjson": "^2.2.1"

},
"author": "Tyler McGinnis, Ben Adam",
"license": "MIT"
}