-
Notifications
You must be signed in to change notification settings - Fork 10
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
Feature: create search params hooks #75
Conversation
Do you also think it's necessary to include search parameter modification methods like |
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.
Update your branch with the latest changes in the repo. Now, you only must provide the hook in TypeScript (like you sugested 🙌) and all hooks are in the hooks
directory.
I do not think we must include it because like you said normally the project's router handles this. |
src/hooks/ts/useSearchParams.ts
Outdated
|
||
type TUseSearchParams = ( url?: string, opt?: { unique: boolean } ) => Record<string, any> | ||
export const useSearchParams: TUseSearchParams = ( url = location.search, opt = { unique: true } ) => { | ||
const _urlSearch = new URL(url) |
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.
When I use the hook without specify an url this error ocurrs:
useSearchParams.ts:5 Uncaught TypeError: Failed to construct 'URL': Invalid URL at useSearchParams (useSearchParams.ts:5:22)
import { useSearchParams } from "./hooks/ts/useSearchParams";
const AppTs = () => {
const params = useSearchParams();
return (
<div>
<p>Typescript test:</p>
{Object.entries(params)?.map(([key, value]) => (
<p key={key}>
<span>{key}</span>: <span>{value}</span>
</p>
))}
</div>
);
};
export default AppTs;
fc0d49a
to
acc4b58
Compare
Ready, I have resolved the conflicts and corrected the current path error. |
src/hooks/js/useSearchParams.js
Outdated
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.
Delete this file and the js folder
src/hooks/useSearchParams.ts
Outdated
@@ -0,0 +1,19 @@ | |||
import { useEffect, useState } from "react" | |||
|
|||
type TUseSearchParams = ( url?: string, opt?: { unique: boolean } ) => Record<string, any> |
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.
Great work @alnavarrop99! 🙌 |
Description:
Hook for reading the search parameters of the route as an argument or of the current route.
Uses Cases:
useSearchParams()
: current url search params.useSearchParams("https://example.com?foo=1&bar=2&foo=2")
: foo: 1, bar: 2.useSearchParams("https://example.com?foo=1&bar=2&foo=2", { unique: false })
: foo: [1,2], bar: 2.Examples: