Comments (10)
BTW: could the "first" initial values be set using (useHydrateAtoms
)[https://github.com/pmndrs/jotai/blob/main/src/react/utils/useHydrateAtoms.ts] and only subsequent changes to the initial value would be done through useEffect
?
from form-atoms.
Hmm that's a reasonable case. I think I might have used useHydrateAtoms
in an older version but the API didn't work with things like the the store.get(field.dirty)
check. You cool to submit a PR?
from form-atoms.
Should the expected behavior for going from defined to undefined be that the value clears? It seems so, right? In that case the check would have to go
from form-atoms.
I played around with removing initialValue === undefined
, turns out would cause issues if one uses useFieldInitialValue
in combination with e.g. useInputField
as the latter would issue a call to useFieldInitialValue
with initialValue=undefined
.
So we would need another value as a "reset" signal, how about RESET
?
export function useFieldInitialValue<Value>(
fieldAtom: FieldAtom<Value>,
initialValue?: Value | typeof RESET,
options?: UseAtomOptions
): UseFieldInitialValue {
const field = useAtomValue(fieldAtom, options);
const store = useStore(options);
React.useEffect(() => {
if (initialValue === undefined) {
return;
}
if (!store.get(field.dirty)) {
store.set(field.value, initialValue);
}
store.set(field._initialValue, initialValue);
}, [store, field._initialValue, field.value, field.dirty, initialValue]);
}
from form-atoms.
what's the use case?
from form-atoms.
i think the biggest annoyance would be forcing people to use useMemo
on their initial values (objects, arrays, etc) but i don't recall what other reason i had if there was one
from form-atoms.
I have an edit form where the data is loaded through react-query which uses an stale-while-revalidate caching strategy. This means the form is populated with possibly outdated data, then the data is reloaded in the background while the form is already mounted and then some fields' initial value may be updated.
from form-atoms.
do you think that equality check I added in the above code is necessary?
from form-atoms.
🎉 This issue has been resolved in version 3.2.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from form-atoms.
@schiller-manuel I'm revisiting the hydration as the current hook fails to initialize the form when rendered on the server (Next.js)
BTW: could the "first" initial values be set using (
useHydrateAtoms
)[https://github.com/pmndrs/jotai/blob/main/src/react/utils/useHydrateAtoms.ts] and only subsequent changes to the initial value would be done throughuseEffect
?
https://github.com/form-atoms/field/blob/main/src/hooks/use-hydrate-field/useHydrateField.ts
from form-atoms.
Related Issues (20)
- Number values cannot be used safely with `<InputField />` nor `useInputField` HOT 23
- Set debug labels for the form atoms HOT 3
- Add `clear` action to support controlling `input[type='file']` HOT 3
- Permit array as root form fields HOT 1
- Use atom id instead of 'unnamed-field' in debugLabels HOT 2
- When input is `required` the `_validateCount` increases while the validate function is not being called HOT 9
- Debug label not set in vite HOT 3
- Pass setter to the `Validate` callback.
- Outdated lockfile? HOT 3
- Release workflow expression syntax error
- Export `validateAtoms` on `formAtom` similarly to `_validateCallback` on `fieldAtom` HOT 1
- Hydration with `useFieldInitialValue` in Next.js renders twice - first empty, then filled input. HOT 21
- onReset callback HOT 6
- Field & Form atoms should be of `PrimitiveAtom` type HOT 4
- Debug label completion HOT 1
- Examples: Validation Option Valibot along with zod HOT 3
- Bug - the `useField` reads value before initialization HOT 1
- Why the additional re-render? HOT 6
- Question: use with Jotai Effect HOT 2
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 form-atoms.