Skip to content

Commit f594fa2

Browse files
authored
Merge pull request #192 from atomic-state/enhancements/useFetch
enh(useFetch, ):
2 parents befe463 + f774c22 commit f594fa2

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-react",
3-
"version": "3.6.2",
3+
"version": "3.6.3",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/use-fetch.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
getRequestHeaders,
6363
hasBaseUrl,
6464
isDefined,
65+
isFormData,
6566
isFunction,
6667
jsonCompare,
6768
notNull,
@@ -1542,18 +1543,23 @@ export function useFetch<FetchDataType = any, BodyType = any>(
15421543

15431544
const submit = useCallback(
15441545
(form: FormData) => {
1545-
if (formRef.current) {
1546-
if (onSubmit) {
1547-
if (onSubmit !== 'reset') {
1548-
onSubmit(formRef.current, form)
1549-
} else {
1550-
if (formRef.current) {
1551-
formRef.current.reset()
1546+
if (isFormData(form)) {
1547+
if (formRef.current) {
1548+
if (onSubmit) {
1549+
if (onSubmit !== 'reset') {
1550+
onSubmit(formRef.current, form)
1551+
} else {
1552+
if (formRef.current) {
1553+
formRef.current.reset()
1554+
}
15521555
}
15531556
}
15541557
}
15551558
}
1556-
temporaryFormData.set(resolvedKey, form)
1559+
temporaryFormData.set(
1560+
resolvedKey,
1561+
isFormData(form) ? form : serialize(form)
1562+
)
15571563
reValidate()
15581564
},
15591565
[resolvedKey, formRef.current, reValidate]

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export {
8282
actionResult,
8383
actionData,
8484
$form,
85+
$formData,
86+
$searchParams,
8587
jsonCompare,
86-
windowExists
88+
windowExists,
89+
queue
8790
} from './utils/shared'

src/utils/shared.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,29 @@ export function $form<T = any>(form: FormData) {
185185
return Object.fromEntries(form.entries()) as T
186186
}
187187

188+
export const $formData = $form
189+
190+
export function $searchParams<T = any>(input: string) {
191+
const searchParams = new URL(input).searchParams
192+
193+
const allKeys = searchParams.keys()
194+
195+
const parsedParams = new Map()
196+
197+
// @ts-expect-error
198+
for (let key of allKeys) {
199+
const allValues = searchParams.getAll(key)
200+
201+
if (allValues.length > 1) {
202+
parsedParams.set(key, allValues)
203+
} else {
204+
parsedParams.set(key, allValues[0])
205+
}
206+
}
207+
208+
return Object.fromEntries(parsedParams.entries()) as T
209+
}
210+
188211
/**
189212
* Creates a new request function. This is for usage with fetcher and fetcher.extend
190213
*/

0 commit comments

Comments
 (0)