Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const IndexPage: FunctionComponent = ({ location }) => {
const [query, setQuery] = useQueryParam(
"query",
StringParam,
parse(location)
);
const handleOnChange = (event: ChangeEvent) => {
setQuery(event.target.value);
};
return (
<div>
<title>Learn Anything</title>
{/* </div>
rawQuery = React.useMemo(() => {
let pathname = {};
// handle checking SSR (#13)
if (locationIsObject) {
// in browser
if (windowIsDefined) {
pathname = parseQueryString(location.search);
} else {
// not in browser
let url = location.pathname;
if (location.search) {
url += location.search;
}
pathname = parseQueryURL(url).query;
}
}
return pathname || {};
}, [location.search, location.pathname, locationIsObject, windowIsDefined]);
}
return pathname || {};
}, [location.search, location.pathname, locationIsObject, windowIsDefined]);
}
// read in the encoded string value
const encodedValue = rawQuery[name];
// note that we use the stringified encoded value since the encoded
// value may be an array that is recreated if a different query param
// changes. It is sufficient to use this instead of encodedValue in
// the useMemo dependency array since it will change any time the actual
// meaningful value of encodedValue changes.
const arraySafeEncodedValue =
encodedValue instanceof Array
? stringify({ [name]: encodedValue })
: encodedValue;
// decode if the encoded value has changed, otherwise
// re-use memoized value
const decodedValue = React.useMemo(() => {
if (encodedValue == null) {
return undefined;
}
return paramConfig.decode(encodedValue);
}, [arraySafeEncodedValue, paramConfig]); // eslint-disable-line react-hooks/exhaustive-deps
// create the setter, memoizing via useCallback
const setValue = React.useCallback(
(newValue: D, updateType?: UrlUpdateType) => {
const newEncodedValue = paramConfig.encode(newValue);
(changes: Partial>, updateType?: UrlUpdateType) => {
// encode as strings for the URL
const encodedChanges: EncodedQueryWithNulls = encodeQueryParams(
paramConfigMap,
changes
);
// update the URL
updateUrlQuery(
encodedChanges,
refHistory.current.location || refLocation.current, // see #46
refHistory.current,
updateType
);
},
[paramConfigMap]
rawQuery = React.useMemo(() => {
let pathname = {};
// handle checking SSR (#13)
if (locationIsObject) {
// in browser
if (windowIsDefined) {
pathname = parseQueryString(location.search);
} else {
// not in browser
let url = location.pathname;
if (location.search) {
url += location.search;
}
pathname = parseQueryURL(url).query;
}
}
return pathname || {};
}, [location.search, location.pathname, locationIsObject, windowIsDefined]);
}
export function calledReplaceQuery(
history: ReturnType,
index: number = 0
) {
return parseQueryString(history.replace.mock.calls[index][0].search);
}
export function calledPushQuery(
history: ReturnType,
index: number = 0
) {
return parseQueryString(history.push.mock.calls[index][0].search);
}
export function makeMockLocation(query: EncodedQueryWithNulls): Location {
const queryStr = stringify(query);
return {
protocol: 'http:',
host: 'localhost',
pathname: '/',
search: queryStr.length ? `?${queryStr}` : '',
} as Location;
}
const rawQuery = React.useMemo(() => parseQueryString(search) || {}, [
search,
function IndexPage({ location }) {
const [query, setQuery] = useQueryParam(
'query',
StringParam,
parse(location.search),
)
const results = useSearch(query || '')
return (
<div>
</div>