Skip to content
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

How to set the initial values of a form? #16

Closed
emi2k01 opened this issue Sep 13, 2022 · 1 comment
Closed

How to set the initial values of a form? #16

emi2k01 opened this issue Sep 13, 2022 · 1 comment

Comments

@emi2k01
Copy link

emi2k01 commented Sep 13, 2022

I want to populate a form with values I get dynamically (from a http request, etc).

Is the only option using <Field> or can I pass values through a <Provider>-like component

EDIT: Ended up doing something like

const fooAtom = fieldAtom({ value: 0 });

const fields = {
  foo: fooAtom,
}
type FormFields = typeof fields;

const myFormAtom = formAtom(fields);

interface FormProps {
  initialValues: {
    [Field in keyof FormFields]: any,
  }
}

const Form = ({ initialValues }) => {
  useFieldAtomInitialValue(fooAtom, initialValues.foo);
  return <form>...</form>
}
@jaredLunde
Copy link
Collaborator

Yeah unfortunately due to the constraints of Jotai your solution and creating your form/field atom dynamically in your components are the only two options.

@emi2k01 emi2k01 closed this as completed Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants