Skip to content
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

Merged
merged 6 commits into from
May 26, 2024

Conversation

alnavarrop99
Copy link
Contributor

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:

import { useSearchParams } from "./hooks/ts/useSearchParams";

const App = () => {
  const params = useSearchParams("https://example.com?foo=1&bar=2&foo=2", { unique: true })
  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 App;

@alnavarrop99
Copy link
Contributor Author

Do you also think it's necessary to include search parameter modification methods like has, set, append, delete that interact directly with the route? I'm not sure if it's a good idea since in most cases the project's router handles this, and I'm not completely sure if there could be incompatibilities between the hook and the router. Moxilla

@dlcastillop dlcastillop linked an issue May 22, 2024 that may be closed by this pull request
Copy link
Collaborator

@dlcastillop dlcastillop left a 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.

@dlcastillop
Copy link
Collaborator

Do you also think it's necessary to include search parameter modification methods like has, set, append, delete that interact directly with the route? I'm not sure if it's a good idea since in most cases the project's router handles this, and I'm not completely sure if there could be incompatibilities between the hook and the router. Moxilla

I do not think we must include it because like you said normally the project's router handles this.


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)
Copy link
Collaborator

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;

@alnavarrop99
Copy link
Contributor Author

Ready, I have resolved the conflicts and corrected the current path error.

Copy link
Collaborator

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 Show resolved Hide resolved
@@ -0,0 +1,19 @@
import { useEffect, useState } from "react"

type TUseSearchParams = ( url?: string, opt?: { unique: boolean } ) => Record<string, any>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also getting a lot of problems with the types

image

Does not your linter show it to you?

@dlcastillop
Copy link
Collaborator

Great work @alnavarrop99! 🙌

@dlcastillop dlcastillop merged commit 5d61051 into novajslabs:main May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hook to get url params
2 participants