Skip to content

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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
replace JSON stringify/parse with superjson
  • Loading branch information
anthonyhoegberg committed Jun 24, 2024
commit daa1170ec94592028457a55a439217f9d8934818
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