Last active
May 12, 2022 01:41
-
-
Save ashimon83/c99e5af7fca9bd26a598fdfcd3a4d442 to your computer and use it in GitHub Desktop.
useFormData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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