-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: useSearchParams
utility hook
#391
Open
junwen-k
wants to merge
4
commits into
molefrog:v3
Choose a base branch
from
junwen-k:feat/use-search-params
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
daec7b0
feat: add useSearchParams hook
junwen-k 3d4b2b9
refactor(hash-location): use navigate from use-browser-location
junwen-k 1823ef6
fix(useSearchParams): stable setSearchParams
junwen-k 972b05b
fix(useSearchParams): use useEvent and remove memo
junwen-k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { renderHook, act } from "@testing-library/react"; | ||
import { | ||
useSearchParams, | ||
Router, | ||
BaseLocationHook, | ||
BaseSearchHook, | ||
} from "wouter"; | ||
import { navigate } from "wouter/use-browser-location"; | ||
import { it, expect, beforeEach, vi } from "vitest"; | ||
|
||
beforeEach(() => history.replaceState(null, "", "/")); | ||
|
||
it("returns browser url search params", () => { | ||
history.replaceState(null, "", "/users?active=true"); | ||
const { result } = renderHook(() => useSearchParams()); | ||
const [value] = result.current; | ||
|
||
expect(value.get("active")).toEqual("true"); | ||
}); | ||
|
||
it("can be customized in the Router", () => { | ||
const customSearchHook: BaseSearchHook = ({ customOption = "unused" }) => | ||
"hello=world"; | ||
const navigate = vi.fn(); | ||
const customHook: BaseLocationHook = () => ["/foo", navigate]; | ||
|
||
const { result } = renderHook(() => useSearchParams(), { | ||
wrapper: (props) => { | ||
return ( | ||
<Router hook={customHook} searchHook={customSearchHook}> | ||
{props.children} | ||
</Router> | ||
); | ||
}, | ||
}); | ||
|
||
expect(result.current[0].get("hello")).toEqual("world"); | ||
|
||
act(() => result.current[1]("active=false")); | ||
expect(navigate).toBeCalledTimes(1); | ||
expect(navigate).toBeCalledWith("?active=false", undefined); | ||
}); | ||
|
||
it("unescapes search string", () => { | ||
const { result } = renderHook(() => useSearchParams()); | ||
|
||
act(() => result.current[1]("?nonce=not Found&country=საქართველო")); | ||
expect(result.current[0].get("nonce")).toBe("not Found"); | ||
expect(result.current[0].get("country")).toBe("საქართველო"); | ||
|
||
// question marks | ||
act(() => result.current[1]("?вопрос=как дела?")); | ||
expect(result.current[0].get("вопрос")).toBe("как дела?"); | ||
}); |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some ideas to reduce extra React imports and make this function smaller:
Here, I replaced
useCallback
withuseEvent
. We should not see degraded performance, unless the component that uses it renders too often, e.g. once every 50-300 ms (but that isn't really our responsibility, since when used standalone this hook will re-render only when the query string changes, which in real-life scenarios shouldn't be too often). Plus,useEvent
is already part ofreact-deps.js
.I'm still trying to figure out how
useMemo
can be avoided further on. The question is whether we really want to memoise it. Considering, that the hook doesn't render too often, it should be safe to always return a new instance ofURLSearchParams
. Why:URLSearchParams
. I'm not sure how browsers implement it, but at the first glance it doesn't seem like a heavy resource. Though, I will make a proper research to figure this out.searchParams
instance is further passed down to components as a prop. But in reality, why would someone want to do that? I assume that the normal use-case is the get the object, and then extract the required primitive objects from it, e.g.searchParams.get("foo")
Anyways, there is a trade-off that we should evaluate there.