Skip to content

Commit

Permalink
docs: Using Props With Modular Forms (#7103)
Browse files Browse the repository at this point in the history
  • Loading branch information
jermsam authored Nov 27, 2024
1 parent c8aa847 commit daa933e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3473,4 +3473,4 @@
"mdFile": "qwik.webviewhtmlattributes.md"
}
]
}
}
29 changes: 29 additions & 0 deletions packages/docs/src/routes/docs/integrations/modular-forms/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,35 @@ export default component$(() => {
```
</CodeSandbox>

## Dealing With Props

Sometimes, you may need to initialize or update the state of your form from the parent component via props. In such cases, you can use the setValue function, as illustrated in the example below:

```tsx
import { setValue, useForm } from "@modular-forms/qwik";

export interface FormProps {
login?: LoginForm
}

export default component$<FormProps>((props) => {
const [loginForm, { Form, Field }] = useForm<LoginForm>({
// rest of the code...
});

useTask$(({ track }) => {
const login = track(() => props.login);
if (!login) return;
for (const [key,value] of Object.entries(login)) {
setValue(loginForm, key, value);
}
});

// rest of the code...
});
```
By tracking props.login with useTask$, the form fields are dynamically updated whenever the parent provides new values.

## Summary

You have learned the basics of Modular Forms and are ready to create your first simple form. For more info and details you can find more guides and the API reference on our website: [modularforms.dev](https://modularforms.dev/)
Expand Down

0 comments on commit daa933e

Please sign in to comment.