usehooks icon indicating copy to clipboard operation
usehooks copied to clipboard

useLocalStorage converts Infinity to null

Open SteveVanOpstal opened this issue 1 year ago • 4 comments

const [number, setNumber] = useLocalStorage("number", Infinity);
console.log(number); // null

Seems to be related to JSON.stringify.

You can workaround this issue by not using Infinity but it would be nice to use this interface as you expect it.

SteveVanOpstal avatar Oct 14 '23 08:10 SteveVanOpstal

I disagree that this belongs here as an issue. This is exactly how the standard was defined to behave:

https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript

ngoue avatar Oct 17 '23 18:10 ngoue

Related: https://github.com/uidotdev/usehooks/issues/253

tylermcginnis avatar Oct 23 '23 16:10 tylermcginnis

I disagree that this belongs here as an issue. This is exactly how the standard was defined to behave:

https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript

It's not necessarily a software issue but coming across this I had the expectation, given that it's a blackbox function, that there were no restrictions. Only after looking at the source code I realised that it uses JSON.stringify to convert it to a string for localStorage, hence it is not able to handle Infinity and NaN. It might just come down to mentioning this in the docs.

SteveVanOpstal avatar Oct 25 '23 06:10 SteveVanOpstal

I agree with @SteveVanOpstal here: the use of JSON.stringify adds further constraints to the already existing constraints in place when using localStorage.setItem.

If, for example, you decide to send in an object and thus get [object Object] back, that's on you for not supplying a primitive, same as if you were using .setItem directly. It isn't the place of a hook to serialise input, particularly when there is no corresponding deserialising of the output; there is no JSON.parse wrapping localStorage.getItem in getLocalStorageItem...

philg-ygt avatar Nov 17 '23 10:11 philg-ygt