Skip to content

Instantly share code, notes, and snippets.

@mjanssen
Last active March 31, 2020 11:11
Show Gist options
  • Save mjanssen/f65f95c49c758097490e886cd1a948bd to your computer and use it in GitHub Desktop.
Save mjanssen/f65f95c49c758097490e886cd1a948bd to your computer and use it in GitHub Desktop.
Get data based on url params
import React, { Component } from 'react';
import dlv from 'dlv';
function getUrlQueryData() {
const queryData = window.location.search.replace(/^\?/, '').split('&');
const d = {};
if (queryData) {
queryData.forEach(k => {
const [key, value] = k.split('=');
d[key] = value;
})
}
return d;
}
function SignUp() {
const [data, setData] = useState(null);
useEffect(() => {
const queryData = getUrlQueryData();
const type = dlv(queryData, 'type', null);
const id = dlv(queryData, 'id', null);
if (type && id) {
// POST to back-end for data
fetch('', { method: 'POST', body: JSON.stringify({ type, id }) }.then(res => res.json()).then(res => {
if (res.data) setData(res.data);
});
}
}, []);
return null;
}
export default SignUp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment