Created
June 8, 2023 18:48
-
-
Save theptrk/db4291fc01509cbebd0ae723f09ef5df to your computer and use it in GitHub Desktop.
react-typescript-form-onsubmit
This file contains hidden or 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
const TagSearchPage: NextPage<{ query: string }> = ({ query }) => { | |
const router = useRouter(); | |
const [input, setInput] = useState(query); | |
const newPath = `/search/${input}`; | |
const title = `Search: ${query}`; | |
const { data, isLoading } = api.search.getAll.useQuery({ | |
query, | |
}); | |
function handleSubmit(e: React.FormEvent<HTMLFormElement>) { | |
e.preventDefault(); | |
void router.push(newPath); | |
} | |
return ( | |
<> | |
<Head> | |
<title>{title}</title> | |
</Head> | |
<PageLayout> | |
<h1 className="text-xl">Search View</h1> | |
<form | |
className="mb-5 mt-2 flex flex-row" | |
onSubmit={(e) => handleSubmit(e)} | |
> | |
<input | |
type="text" | |
className="mx-1 w-full rounded-sm border bg-transparent p-2" | |
value={input} | |
onChange={(e) => setInput(e.target.value)} | |
/> | |
<button type="submit" className="mx-1 rounded-sm border px-2 py-1"> | |
Search | |
</button> | |
</form> | |
{isLoading ? ( | |
<div>Loading...</div> | |
) : ( | |
data?.map(({ note, author }) => ( | |
<NoteView note={note} author={author} key={note.id} /> | |
)) | |
)} | |
</PageLayout> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment