How to use serialize-query-params - 10 common examples

To help you get started, we’ve selected a few serialize-query-params examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github learn-anything / learn-anything / pages / index.tsx View on Github external
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>
github pbeshai / use-query-params / src / useQueryParam.ts View on Github external
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]);
  }
github pbeshai / use-query-params / src / useQueryParam.ts View on Github external
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);
github pbeshai / use-query-params / src / useQueryParams.ts View on Github external
(changes: Partial&gt;, updateType?: UrlUpdateType) =&gt; {
      // 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]
github pbeshai / use-query-params / src / useQueryParam.ts View on Github external
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]);
  }
github pbeshai / use-query-params / src / __tests__ / helpers.ts View on Github external
export function calledReplaceQuery(
  history: ReturnType,
  index: number = 0
) {
  return parseQueryString(history.replace.mock.calls[index][0].search);
}
github pbeshai / use-query-params / src / __tests__ / helpers.ts View on Github external
export function calledPushQuery(
  history: ReturnType,
  index: number = 0
) {
  return parseQueryString(history.push.mock.calls[index][0].search);
}
github pbeshai / use-query-params / src / __tests__ / helpers.ts View on Github external
export function makeMockLocation(query: EncodedQueryWithNulls): Location {
  const queryStr = stringify(query);
  return {
    protocol: 'http:',
    host: 'localhost',
    pathname: '/',
    search: queryStr.length ? `?${queryStr}` : '',
  } as Location;
}
github pbeshai / use-query-params / src / useQueryParams.ts View on Github external
  const rawQuery = React.useMemo(() => parseQueryString(search) || {}, [
    search,
github feathericons / feathericons.com / src / pages / index.js View on Github external
function IndexPage({ location }) {
  const [query, setQuery] = useQueryParam(
    'query',
    StringParam,
    parse(location.search),
  )
  const results = useSearch(query || '')

  return (
    
      
      <div>
        </div>

serialize-query-params

A library for simplifying encoding and decoding URL query parameters.

ISC
Latest version published 2 years ago

Package Health Score

58 / 100
Full package analysis