Skip to content

Commit

Permalink
fix(reValidate):
Browse files Browse the repository at this point in the history
Fixes revalidate not working properly with server actions
  • Loading branch information
danybeltran committed May 19, 2024
1 parent 7e0fa31 commit 5edc30f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/hooks/others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ export function useServerAction<T extends (args: any) => any>(
) {
let mockServerActionId = config?.id ?? getMockServerActionId(action)

const $action = useFetch(action.name, {
const $action = useFetch(mockServerActionId, {
fetcher: async function proxied(_, config) {
const actionParam = actionForms.get(mockServerActionId) ?? config?.params

Expand Down
8 changes: 7 additions & 1 deletion src/hooks/use-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(

const reValidate = useCallback(
async function reValidate() {
if (!isPending(resolvedKey) && !loading) {
if (!isPending(resolvedKey)) {
revalidate(id)
}
},
Expand Down Expand Up @@ -1476,6 +1476,10 @@ export function useFetch<FetchDataType = any, BodyType = any>(
})
}

const refreshRequest = () => {
revalidate(id)
}

return {
get resetError() {
thisDeps.error = true
Expand Down Expand Up @@ -1555,6 +1559,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
thisDeps.loading = true
return statusCodes.get(resolvedKey)
},
refresh: refreshRequest,
get reFetch() {
thisDeps.loading = true
return reValidate
Expand Down Expand Up @@ -1595,6 +1600,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
*/
key: resolvedKey
} as unknown as {
refresh(): void
resetError(): void
formProps: {
action: (form: FormData) => Promise<void>
Expand Down

0 comments on commit 5edc30f

Please sign in to comment.