How to use the serialize-query-params.stringify function in serialize-query-params

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 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 / __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;
}

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