This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get query parameters for preview
* add a custom hook for grabbing query parameters off the location object * use hook in preview page to grab and log out the 'pr' and 'quickstart' query parameters
- Loading branch information
1 parent
3993ff3
commit 6d278b9
Showing
2 changed files
with
37 additions
and
0 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,20 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* A hook to grab query parameters off of the current URL | ||
* @param {string} parameterKey - The key for the query parameter | ||
* @param {object} location - The Gatsby location object | ||
* @returns {string|null} The string value of the parameter or null if it does not exist | ||
*/ | ||
const useQueryParameter = (parameterKey, location) => { | ||
const [param, setParam] = useState(null); | ||
|
||
useEffect(() => { | ||
const urlParams = new URLSearchParams(location.search); | ||
setParam(urlParams.get(parameterKey)); | ||
}, [parameterKey, location]); | ||
|
||
return param; | ||
}; | ||
|
||
export default useQueryParameter; |
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,17 @@ | ||
import PropTypes from 'prop-types'; | ||
import useQueryParameter from '../hooks/useQueryParameter'; | ||
|
||
const PreviewPage = ({ location }) => { | ||
const prNumber = useQueryParameter('pr', location); | ||
const quickstartPath = useQueryParameter('quickstart', location); | ||
|
||
console.log(prNumber); | ||
console.log(quickstartPath); | ||
return <span>oh hai</span>; | ||
}; | ||
|
||
PreviewPage.propTypes = { | ||
location: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default PreviewPage; |