mantine form values generic types #7195
Answered
by
ryuhangyeong
ryuhangyeong
asked this question in
Q&A
-
https://mantine.dev/form/use-form/#useformreturntype In my requirements, I need to receive various form types from the component. For this purpose, we used the generic method below. Type cannot be inferred. Could you please help? Examples// hooks/use-test-form.ts
import { useForm } from "@mantine/form";
export type UseTestFormValues = {
name: string;
age: number;
};
export const useTestForm = () => {
const form = useForm<UseTestFormValues>({
initialValues: {
name: "",
age: 0,
},
});
return {
form,
};
}; // App.tsx
import Test from "./components/Test";
import { useTestForm, UseTestFormValues } from "./hooks/use-test-form";
const App = () => {
const { form } = useTestForm();
return (
<div>
<Test<UseTestFormValues> form={form} />
</div>
);
};
export default App; // components/Test.tsx
import { UseFormReturnType } from "@mantine/form";
type TestProps<T> = {
form: UseFormReturnType<T>;
};
const Test = <T,>({ form }: TestProps<T>) => {
console.log(form.values.name); // **type error**
return <div>hello world</div>;
};
export default Test; |
Beta Was this translation helpful? Give feedback.
Answered by
ryuhangyeong
Nov 30, 2024
Replies: 1 comment
-
import { UseFormReturnType } from "@mantine/form";
type TestProps<T> = {
form: UseFormReturnType<T>;
};
const Test = <T extends Record<string, PropertyKey>>({
form,
}: TestProps<T>) => {
console.log(form.values.name);
return <div>hello world</div>;
};
export default Test I found this method...it's not good |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ryuhangyeong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this method...it's not good