Skip to content

Instantly share code, notes, and snippets.

@fernandoabolafio
Last active September 7, 2019 12:11
Show Gist options
  • Save fernandoabolafio/7c8515ddd6dd13184cbebd5fbabc1218 to your computer and use it in GitHub Desktop.
Save fernandoabolafio/7c8515ddd6dd13184cbebd5fbabc1218 to your computer and use it in GitHub Desktop.
useQueryString
import { useState, useCallback } from "react";
import { getQueryStringValue, setQueryStringValue } from "./queryString";
function useQueryString(key, initialValue) {
const [value, setValue] = useState(getQueryStringValue(key) || initialValue);
const onSetValue = useCallback(
newValue => {
setValue(newValue);
setQueryStringValue(key, newValue);
},
[key]
);
return [value, onSetValue];
}
export default useQueryString;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment