This repository was archived by the owner on Feb 14, 2025. It is now read-only.
-
-
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.
* Added generic parameter for state type * Added missing Generic type * Added generic type parameter to the hooks and some examples * docs(changeset): Added generic type parameter to the useSearchParamsState hooks
- Loading branch information
1 parent
4695758
commit c6f9574
Showing
11 changed files
with
243 additions
and
228 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@use-search-params-state/react": patch | ||
"@use-search-params-state/next": patch | ||
--- | ||
|
||
Added generic type parameter to the useSearchParamsState hooks |
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,5 +1,5 @@ | ||
import { redirect } from "next/navigation"; | ||
|
||
export default function IndexPage() { | ||
redirect("/default"); | ||
redirect("/basic"); | ||
} |
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,35 @@ | ||
"use client"; | ||
|
||
import { SetKeyValueInputs } from "@/components/set-key-value-inputs"; | ||
import { Alert } from "@/components/ui/alert"; | ||
|
||
import { useSearchParamsState } from "@use-search-params-state/next"; | ||
|
||
interface SearchParamsType { | ||
page: string; | ||
search?: string; | ||
} | ||
|
||
export default function Page() { | ||
const [state, setState] = useSearchParamsState<SearchParamsType>({ | ||
defaultValues: { | ||
page: "1", | ||
}, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<Alert> | ||
{ | ||
"Check source code for this page. It has a generic type parameter passed to useSearchParamsState and enforces usage of the state inline with the provided type." | ||
} | ||
</Alert> | ||
|
||
<SetKeyValueInputs | ||
setState={(k, v) => setState(k as keyof SearchParamsType, v)} | ||
/> | ||
|
||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
</div> | ||
); | ||
} |
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,3 +1,3 @@ | ||
export default function Page() { | ||
return <div>with zod</div>; | ||
return <div>TODO: with zod</div>; | ||
} |
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,18 @@ | ||
import { useSearchParamsState } from "./index"; | ||
|
||
const Component = () => { | ||
const [state, setState] = useSearchParamsState<{ | ||
greeting?: string; | ||
hello: string; | ||
}>(); | ||
|
||
const handleClick = () => { | ||
state.greeting === "hello" | ||
? setState("greeting", "world") | ||
: setState("greeting", "hello"); | ||
}; | ||
|
||
return <button onClick={handleClick}>{state.greeting ?? "hello"}</button>; | ||
}; | ||
|
||
<Component />; |
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 { useState } from "react"; | ||
|
||
import { useSearchParamsState } from "./index"; | ||
|
||
const Component1 = () => { | ||
const [searchParams, setSearchParams] = useState(new URLSearchParams()); | ||
|
||
const [state, setState] = useSearchParamsState<{ | ||
greeting?: string; | ||
hello: string; | ||
}>({ | ||
searchParams, | ||
setSearchParams: (newSearchParams) => { | ||
console.log("newSearchParams", newSearchParams); | ||
setSearchParams(newSearchParams); | ||
}, | ||
}); | ||
|
||
const handleClick = () => { | ||
state.greeting === "hello" | ||
? setState("greeting", "world") | ||
: setState("greeting", "hello"); | ||
}; | ||
|
||
return <button onClick={handleClick}>{state.greeting ?? "hello"}</button>; | ||
}; | ||
|
||
<Component1 />; | ||
|
||
const Comp2 = () => { | ||
const [searchParams, setSearchParams] = useState(new URLSearchParams()); | ||
|
||
const [state, setState] = useSearchParamsState<{ | ||
page: string; | ||
hello: string; | ||
}>({ | ||
defaultValues: { | ||
hello: "world", | ||
}, | ||
searchParams, | ||
setSearchParams: (newSearchParams) => { | ||
console.log("newSearchParams", newSearchParams); | ||
setSearchParams(newSearchParams); | ||
}, | ||
}); | ||
|
||
const handleClick = () => { | ||
state.page === "1" ? setState("page", "2") : setState("page", "1"); | ||
}; | ||
|
||
return <button onClick={handleClick}>{state.page}</button>; | ||
}; | ||
|
||
<Comp2 />; |
Oops, something went wrong.