Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 714 Bytes

hooks.md

File metadata and controls

35 lines (25 loc) · 714 Bytes

修改Url的SearchParams(useUpdateSearchParams.ts)

import { useSearchParams } from "next/navigation"
import { useCallback } from "react"


export const useUpdateSearchParams = () => {

    const searchParams = useSearchParams()

    const updateSearchParams = useCallback(
        (name: string, value: string) => {
          const params = new URLSearchParams(searchParams.toString())
          params.set(name, value)
     
          return params.toString()
        },
        [searchParams]
      )


    return { updateSearchParams }
} 

Example

const { updateSearchParams } = useUpdateSearchParams();

<Link
  href={`/?${updateSearchParams("category", "test")}`}
  scroll={false}
>