-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from adamborowski/8-avoid-re-renders
fix: avoid extra renders
- Loading branch information
Showing
11 changed files
with
161 additions
and
181 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
export interface SearchClient<Entity> { | ||
search: ( | ||
query: string, | ||
abortController?: AbortController | ||
) => Promise<Entity[]>; | ||
search: (query: string, signal?: AbortSignal) => Promise<Entity[]>; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { PromiseState } from "../state/usePromiseState"; | ||
import CancelablePromise from "cancelable-promise"; | ||
import { cancelableWithAbortController } from "../state/cancelablePromise"; | ||
|
||
export const useQuery = <Data>( | ||
getData: (signal: AbortSignal) => Promise<Data> | ||
) => { | ||
const [state, setState] = useState<PromiseState<Data>>({ | ||
type: "pending", | ||
}); | ||
const lastPromise = useRef<CancelablePromise | null>(null); | ||
const lastState = useRef(state); | ||
|
||
const reload = useCallback(async () => { | ||
const abortController = new AbortController(); | ||
lastPromise.current?.cancel(); | ||
lastState.current.type !== "pending" && setState({ type: "pending" }); // avoid extra render, read state by ref | ||
try { | ||
lastPromise.current = cancelableWithAbortController( | ||
abortController, | ||
getData(abortController.signal) | ||
); | ||
|
||
const fetchedResult = await lastPromise.current; | ||
setState({ type: "loaded", data: fetchedResult }); | ||
} catch (e) { | ||
if (e instanceof DOMException && e.name === "AbortError") { | ||
// ignore this error, we are already in loading state due to the new fetch | ||
} else { | ||
setState({ | ||
type: "error", | ||
message: String(e), | ||
}); | ||
} | ||
} | ||
}, [getData]); | ||
|
||
useEffect(() => { | ||
void reload(); | ||
|
||
return () => { | ||
lastPromise.current?.cancel(); | ||
}; | ||
}, [reload]); | ||
|
||
lastState.current = state; | ||
|
||
return { state, reload }; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.