Comments (5)
As workaround, for now I'm using this wrapper:
import { useRef } from 'react';
import { equals } from 'ramda';
import _createPersistedState from 'use-persisted-state';
const useMemoizedObject = (obj) => {
const mem = useRef();
if (equals(mem.current, obj)) {
return mem.current;
} else {
mem.current = obj;
return obj;
}
};
const createPersistedState = (key) => {
const usePersistedState = _createPersistedState(key);
return (initialValue) => {
const [_value, setValue] = usePersistedState(initialValue);
const value = useMemoizedObject(_value);
return [value, setValue];
};
};
export default createPersistedState;
from use-persisted-state.
As workaround, for now I'm using this wrapper:
import { useRef } from 'react'; import { equals } from 'ramda'; import _createPersistedState from 'use-persisted-state'; const useMemoizedObject = (obj) => { const mem = useRef(); if (equals(mem.current, obj)) { return mem.current; } else { mem.current = obj; return obj; } }; const createPersistedState = (key) => { const usePersistedState = _createPersistedState(key); return (initialValue) => { const [_value, setValue] = usePersistedState(initialValue); const value = useMemoizedObject(_value); return [value, setValue]; }; }; export default createPersistedState;
Could you do something like the following for the same result?
import createPersistedState from 'use-persisted-state'
import { useMemo } from 'react'
const useLocalStorage = (key: string, initialValue: any) => {
const usePersistedState = createPersistedState(key)
const [_value, setValue] = usePersistedState(initialValue)
const value = useMemo(() => _value, [_value])
return [value, setValue]
}
export default useLocalStorage
from use-persisted-state.
No, useMemo
doesn't perform a deep comparison
from use-persisted-state.
I did not know that. Thanks!
from use-persisted-state.
I am working on this (https://github.com/dennismorello/use-persisted-state/tree/feature/deep-comparison). I will open a PR when I'm done 👍🏻
from use-persisted-state.
Related Issues (20)
- Is it possible to detect the origin tab of an update? HOT 1
- Error when new value is not JSON HOT 13
- New state isn't persisted if component is hidden before next render HOT 1
- `global` does not exists in browser HOT 1
- Initial state HOT 3
- Passing a function to setState is not supported HOT 1
- Use with AsyncStorage
- Unexpected token c in JSON at position 0 HOT 7
- Is this being maintained? HOT 5
- I've got incompatibility with the storageEvent api in Safari and React Native (might be only on mac devices however)
- When calling setState multiple times, only the last call takes effect HOT 1
- "Uncaught (in promise) TypeError: Cannot read property 'emit' of null" HOT 7
- Encryption and decryption of data in localstorage
- The returned setter function is changed in each render HOT 3
- Uncaught reference error: global is not defined HOT 3
- Uncaught SyntaxError: Unexpected token u in JSON at position 0 HOT 1
- No support for React 18 HOT 3
- Error Dependencies every time I install new package HOT 1
- NPM Package needs updated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-persisted-state.