tiaanduplessis / react-hook-form-persist Goto Github PK
View Code? Open in Web Editor NEWPersist and populate react-hook-form form using storage of your choice
License: MIT License
Persist and populate react-hook-form form using storage of your choice
License: MIT License
The readme shows the following example
useFormPersist('form', {watch, setValue, include: ['email'] });
This option doesn't actually exist
I think it makes sense to add this option because most of the time you want to persist only 1 or 2 fields and not the whole form
Is it possible to add something like include
as well as ignore
to persist certain form data?
Supply include
into watch
(I've renamed ignore
to exclude
to align with the change):
export default (
name,
watch,
setValue,
{ include, exclude, storage = window.sessionStorage } = {}
) => {
const values = watch(include);
useEffect(() => {
const storageItem = storage.getItem(name);
if (storageItem) {
Object.entries(JSON.parse(storageItem)).forEach(([key, value]) => {
if (!exclude?.includes(key)) {
setValue(key, value);
}
});
}
}, [name]);
useEffect(() => storage.setItem(name, JSON.stringify(values)));
return { clear: () => storage.removeItem(name) };
};
// Persist all form fields
useFormPersist('form', watch, setValue);
// Persist all form fields except password
useFormPersist('form', watch, setValue, { exclude: ['password'] });
// Persist only the email field
useFormPersist('form', watch, setValue, { include: ['email'] });
Wanted to double check before I migrate to v7 and NextJS.
Thank you for this useful lib, anw, I tried to passing exclude parameter for array field, so I have array field
setValue(`attributes[${index}].value`, theDate);
but this value not excluding from storage, do you have some suggestions?
I haven't tested this, so I might be completely wrong. However, it might be worth testing whether this hook is vulnerable to prototype pollution. Consider reviewing https://codeburst.io/what-is-prototype-pollution-49482fc4b638
I'm not sure if this is fixable (but I hope it is). After setting up useFormPersist
using the repro steps below, my page where useForm
sits gets rerendered on every field change:
import useFormPersist from 'react-hook-form-persist'
const { setValue, watch } = form;
useFormPersist("form-storage", {
watch,
setValue,
storage: window.localStorage
});
By commenting out the useFormPersist
command above the problem is resolved but obviously the functionality is gone.
Motivation: Measured by Chrome DevTools / Performance tab, there is a 4-fold performance degradation with this command turned on (moving from 100ms to 400-500ms on my test case).
Suggestion: I think that if possible, the form should be persisted without causing the current element to rerender.
Can you upgrade the React dependency to 17?
I was looking at incorporating into an existing FormWizard
component that uses RHF and is used in many places across our application. I’ve added a persistKey
prop (string) that, when present, would just be used to enable form persist within that instance of the wizard. Like this:
if (persistKey && typeof persistKey === 'string') {
useFormPersist(persistKey, {
setValue,
watch,
});
}
However, it’s not recommended to conditionally apply hooks in a component, so what would be the recommended process here?
I was considering forking the project and adding something like this to the two useEffect
blocks:
if (!name) return
Thoughts?
For example: a date is transformed to string, but when restored it's still a string so it errors, same with bigints, it would be great to be able to customize the behavior
Hi,
there is an issue with a specific security condition where this hook can potentially break a whole web application, also production deploys, just by using it in the intended way.
If you integrate a web app inside of an <iframe> but this <iframe> is not served from the same domain, we have a typical "vertical" / "whitelabel" / "intranet integration" / "extranet integration" use-case.
This standard use case can potentially break a whole application and render a white-screen in production when the user is visiting that site in private mode. In this condition, browsers like Chrome and Firefox activate specific security rules that cause a DOMException to happen, only if you just try to read window.sessionStorage / window.localStorage in your code:
This is happening here:
https://github.com/tiaanduplessis/react-hook-form-persist/blob/master/src/index.js#L16
And causes:
...based on production transpiled and minified code from this lib:
react-hook-form-persist.js":function(e,t,n){var r=n("../../node_modules/react/index.js");e.exports=function(e,t,n){var a=t.watch,o=t.setValue,i=void 0===n?{}:n,s=i.storage,l=void 0===s?window.sessionStorage:s,
There are two ways to fix this:
I have that in code already and can PR it.
Are you aware of this thing and would you accept a PR?
I see the data in localStorage, however when I hit refresh, the data is there then it gets cleared before it can get loaded into the form.
useFormPersist(
'form',
{ watch, setValue },
{
storage: window.localStorage,
}
)
SessionStorage works but can't figure out whats wrong with localStorage.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.