Last active
April 13, 2021 21:00
-
-
Save bretmorris/2fbc1bd99144c89433050bab6a081b2b to your computer and use it in GitHub Desktop.
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
import { useRouter } from 'next/router' | |
import { useState, useEffect } from 'react' | |
const Page = () => { | |
const router = useRouter() | |
const [results, setResults] = useState(false) | |
const [query, setQuery] = useState() | |
// react to query parameter changes | |
useEffect(() => { | |
const q = router.query.query | |
setQuery(q) | |
// example: show reults if query === 'test' | |
if (q === 'test') setResults(true) | |
else setResults(false) | |
}, [router.query]) | |
return ( | |
<div> | |
<input | |
type="text" | |
onChange={(e) => { | |
const { pathname } = window.location | |
const url = !e.target.value | |
? pathname | |
: `${pathname}?query=${e.target.value}` | |
// update the current pathname with query param value | |
router.push(url, null, { shallow: true }) | |
}} | |
/> | |
// show results if results === true | |
{results ? <div>results here</div> : <div>no results</div>} | |
</div> | |
) | |
} | |
export default Page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment