#useFormReturnType usage
6 messages · Page 1 of 1 (latest)
If you do not use TypeScript, then simply remove all types
Thanks. Still not sure how to pass in UseFormRerturnType to component. Could you help? Thank you!```import { TextInput } from '@mantine/core';
import { useForm, UseFormReturnType } from '@mantine/form';
function NameInput({ form: UseFormReturnType }) {
return <TextInput {...form.getInputProps('name')} />;
}
function Demo() {
const form = useForm({ initialValues: { name: '', occupation: '' } });
return (
<NameInput form={form} />
);
}```
UseFormReturnType is a type. If your files are javascript (*.js) you do not ever need it. You should be able to copy paste this first example without changing anything since it doesn't use any types. https://mantine.dev/form/use-form/#usage
If you do use typescript, you do not need to explicitly specify the types in this case because TS is a structural type system.
Thanks. I have create CodeSandbox example to illustrate my issue. When passing form to component form fields are not editable if Im trying to getinputProps.https://codesandbox.io/s/optimistic-fermat-um1nbk?file=/src/App.js
Two things
- you do not need to manually provide
value={form.values.*}orchecked=...on the inputs because{...form.getInputProps()}does that already. - The fix for fields not being editable was to move the
FormBodycomponent definition to be outside the otherAppcomponent definition. You should never nest definitions of components like that otherwise you'll get issues with closures (at least I think that is what the issue was).
const FormBody = (props) => {...} // This is good
export default function App() {
const FormBody = (props) => {...} // This is bad
return <div />
}