Reach Router has a useLocation hook you can use:
import { useLocation } from "@reach/router"
// then
const location = useLocation()
In location.search
is the search field, containing all the query parameters (query strings) from your URL. In your example, /home?init=true
would return location.search: "?init=true"
, which is a query string.
You can use query-string library (yarn add query-string
) to parse this:
import { parse } from "query-string"
// then
const searchParams = parse(location.search)
which would give you object
{init: "true"}
you could also parse booleans with queryString.parse('foo=true', {parseBooleans: true})
So the full example would be
import { useLocation } from "@reach/router"
import { parse } from "query-string"
// and
const location = useLocation()
const searchParams = parse(location.search) // => {init: "true"}