Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
feat: get query parameters for preview
Browse files Browse the repository at this point in the history
* 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
aswanson-nr committed May 11, 2022
1 parent 3993ff3 commit 6d278b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/hooks/useQueryParameter.js
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;
17 changes: 17 additions & 0 deletions src/pages/preview.js
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;

0 comments on commit 6d278b9

Please sign in to comment.