Skip to content

Instantly share code, notes, and snippets.

@ashimon83
Last active May 12, 2022 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashimon83/c99e5af7fca9bd26a598fdfcd3a4d442 to your computer and use it in GitHub Desktop.
Save ashimon83/c99e5af7fca9bd26a598fdfcd3a4d442 to your computer and use it in GitHub Desktop.
useFormData
import { useMemo } from 'react';
type Params = {
[key: string]: any;
}
export function useFormData(params: Params): FormData {
const memoizeFormData = useMemo(() => {
const formData = new FormData();
Object.entries(params).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((v) => {
formData.append(key, v);
});
} else {
formData.append(key, value);
}
});
return formData;
}, [params]);
return memoizeFormData;
}
const formData = useFormData({
name,
tel,
password,
});
await registerUser({
params: formData
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment