use-query-params
Advanced tools
Comparing version 1.1.2 to 1.1.3
import * as React from 'react'; | ||
export declare function useUpdateRefIfShallowNew<T>(ref: React.MutableRefObject<T>, newValue: T): void; | ||
export declare function useUpdateRefIfShallowNew<T>(ref: React.MutableRefObject<T>, newValue: T, isEqual?: (objA: NonNullable<T>, objB: NonNullable<T>) => boolean): void; | ||
export declare function getSSRSafeSearchString(location: Location | undefined): string; |
import * as React from 'react'; | ||
import { extract } from 'query-string'; | ||
import shallowEqual from './shallowEqual'; | ||
export function useUpdateRefIfShallowNew(ref, newValue) { | ||
var hasNew = !shallowEqual(ref.current, newValue); | ||
export function useUpdateRefIfShallowNew(ref, newValue, isEqual) { | ||
if (isEqual === void 0) { isEqual = shallowEqual; } | ||
var hasNew = ((ref.current == null || newValue == null) && ref.current === newValue) || | ||
!isEqual(ref.current, newValue); | ||
React.useEffect(function () { | ||
@@ -7,0 +9,0 @@ if (hasNew) { |
@@ -13,1 +13,6 @@ /** | ||
export default function shallowEqual(objA: any, objB: any): boolean; | ||
/** | ||
* @pbeshai modification of shallowEqual to take into consideration a map providing | ||
* equals functions | ||
*/ | ||
export declare function shallowEqualMap(objA: any, objB: any, equalMap: any): boolean; |
@@ -55,1 +55,31 @@ /** | ||
} | ||
/** | ||
* @pbeshai modification of shallowEqual to take into consideration a map providing | ||
* equals functions | ||
*/ | ||
export function shallowEqualMap(objA, objB, equalMap) { | ||
var _a; | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
// Test for A's keys different from B. | ||
for (var i = 0; i < keysA.length; i++) { | ||
var isEqual = (_a = equalMap[keysA[i]].equals) !== null && _a !== void 0 ? _a : is; | ||
if (!hasOwnProperty.call(objB, keysA[i]) || | ||
!isEqual(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
@@ -12,4 +12,6 @@ import * as React from 'react'; | ||
function getLatestDecodedValue(location, name, paramConfig, paramConfigRef, encodedValueCacheRef, decodedValueCacheRef) { | ||
var _a; | ||
// check if we have a new param config | ||
var hasNewParamConfig = !shallowEqual(paramConfigRef.current, paramConfig); | ||
var isValueEqual = (_a = paramConfig.equals) !== null && _a !== void 0 ? _a : shallowEqual; | ||
// read in the parsed query | ||
@@ -33,3 +35,5 @@ var parsedQuery = sharedMemoizedQueryParser(getSSRSafeSearchString(location) // get the latest location object | ||
var newDecodedValue = paramConfig.decode(encodedValue); | ||
var hasNewDecodedValue = !shallowEqual(decodedValueCacheRef.current, newDecodedValue); | ||
var hasNewDecodedValue = ((decodedValueCacheRef.current == null || newDecodedValue == null) && | ||
decodedValueCacheRef.current === newDecodedValue) || | ||
!isValueEqual(decodedValueCacheRef.current, newDecodedValue); | ||
// if we have a new decoded value use it, otherwise use cached | ||
@@ -67,3 +71,3 @@ return hasNewDecodedValue | ||
useUpdateRefIfShallowNew(paramConfigRef, paramConfig); | ||
useUpdateRefIfShallowNew(decodedValueCacheRef, decodedValue); | ||
useUpdateRefIfShallowNew(decodedValueCacheRef, decodedValue, paramConfig.equals); | ||
// create the setter, memoizing via useCallback | ||
@@ -70,0 +74,0 @@ var setValue = React.useCallback(function setValueCallback(newValue, updateType) { |
@@ -6,3 +6,3 @@ import * as React from 'react'; | ||
import { sharedMemoizedQueryParser } from './memoizedQueryParser'; | ||
import shallowEqual from './shallowEqual'; | ||
import shallowEqual, { shallowEqualMap } from './shallowEqual'; | ||
/** | ||
@@ -57,3 +57,3 @@ * Helper to get the latest decoded values with smart caching. | ||
// keep referential equality for decoded valus if we didn't actually change anything | ||
var hasNewDecodedValues = !shallowEqual(decodedValuesCacheRef.current, decodedValues); | ||
var hasNewDecodedValues = !shallowEqualMap(decodedValuesCacheRef.current, decodedValues, paramConfigMap); | ||
return { | ||
@@ -90,3 +90,5 @@ encodedValues: encodedValues, | ||
useUpdateRefIfShallowNew(encodedValuesCacheRef, encodedValues); | ||
useUpdateRefIfShallowNew(decodedValuesCacheRef, decodedValues); | ||
useUpdateRefIfShallowNew(decodedValuesCacheRef, decodedValues, function (a, b) { | ||
return shallowEqualMap(a, b, paramConfigMap); | ||
}); | ||
// create a setter for updating multiple query params at once | ||
@@ -93,0 +95,0 @@ var setQuery = React.useCallback(function (changes, updateType) { |
import * as React from 'react'; | ||
export declare function useUpdateRefIfShallowNew<T>(ref: React.MutableRefObject<T>, newValue: T): void; | ||
export declare function useUpdateRefIfShallowNew<T>(ref: React.MutableRefObject<T>, newValue: T, isEqual?: (objA: NonNullable<T>, objB: NonNullable<T>) => boolean): void; | ||
export declare function getSSRSafeSearchString(location: Location | undefined): string; |
@@ -6,4 +6,6 @@ "use strict"; | ||
var shallowEqual_1 = require("./shallowEqual"); | ||
function useUpdateRefIfShallowNew(ref, newValue) { | ||
var hasNew = !shallowEqual_1.default(ref.current, newValue); | ||
function useUpdateRefIfShallowNew(ref, newValue, isEqual) { | ||
if (isEqual === void 0) { isEqual = shallowEqual_1.default; } | ||
var hasNew = ((ref.current == null || newValue == null) && ref.current === newValue) || | ||
!isEqual(ref.current, newValue); | ||
React.useEffect(function () { | ||
@@ -10,0 +12,0 @@ if (hasNew) { |
@@ -13,1 +13,6 @@ /** | ||
export default function shallowEqual(objA: any, objB: any): boolean; | ||
/** | ||
* @pbeshai modification of shallowEqual to take into consideration a map providing | ||
* equals functions | ||
*/ | ||
export declare function shallowEqualMap(objA: any, objB: any, equalMap: any): boolean; |
@@ -58,1 +58,32 @@ "use strict"; | ||
exports.default = shallowEqual; | ||
/** | ||
* @pbeshai modification of shallowEqual to take into consideration a map providing | ||
* equals functions | ||
*/ | ||
function shallowEqualMap(objA, objB, equalMap) { | ||
var _a; | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
// Test for A's keys different from B. | ||
for (var i = 0; i < keysA.length; i++) { | ||
var isEqual = (_a = equalMap[keysA[i]].equals) !== null && _a !== void 0 ? _a : is; | ||
if (!hasOwnProperty.call(objB, keysA[i]) || | ||
!isEqual(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
exports.shallowEqualMap = shallowEqualMap; |
@@ -14,4 +14,6 @@ "use strict"; | ||
function getLatestDecodedValue(location, name, paramConfig, paramConfigRef, encodedValueCacheRef, decodedValueCacheRef) { | ||
var _a; | ||
// check if we have a new param config | ||
var hasNewParamConfig = !shallowEqual_1.default(paramConfigRef.current, paramConfig); | ||
var isValueEqual = (_a = paramConfig.equals) !== null && _a !== void 0 ? _a : shallowEqual_1.default; | ||
// read in the parsed query | ||
@@ -35,3 +37,5 @@ var parsedQuery = memoizedQueryParser_1.sharedMemoizedQueryParser(helpers_1.getSSRSafeSearchString(location) // get the latest location object | ||
var newDecodedValue = paramConfig.decode(encodedValue); | ||
var hasNewDecodedValue = !shallowEqual_1.default(decodedValueCacheRef.current, newDecodedValue); | ||
var hasNewDecodedValue = ((decodedValueCacheRef.current == null || newDecodedValue == null) && | ||
decodedValueCacheRef.current === newDecodedValue) || | ||
!isValueEqual(decodedValueCacheRef.current, newDecodedValue); | ||
// if we have a new decoded value use it, otherwise use cached | ||
@@ -69,3 +73,3 @@ return hasNewDecodedValue | ||
helpers_1.useUpdateRefIfShallowNew(paramConfigRef, paramConfig); | ||
helpers_1.useUpdateRefIfShallowNew(decodedValueCacheRef, decodedValue); | ||
helpers_1.useUpdateRefIfShallowNew(decodedValueCacheRef, decodedValue, paramConfig.equals); | ||
// create the setter, memoizing via useCallback | ||
@@ -72,0 +76,0 @@ var setValue = React.useCallback(function setValueCallback(newValue, updateType) { |
@@ -58,3 +58,3 @@ "use strict"; | ||
// keep referential equality for decoded valus if we didn't actually change anything | ||
var hasNewDecodedValues = !shallowEqual_1.default(decodedValuesCacheRef.current, decodedValues); | ||
var hasNewDecodedValues = !shallowEqual_1.shallowEqualMap(decodedValuesCacheRef.current, decodedValues, paramConfigMap); | ||
return { | ||
@@ -91,3 +91,5 @@ encodedValues: encodedValues, | ||
helpers_1.useUpdateRefIfShallowNew(encodedValuesCacheRef, encodedValues); | ||
helpers_1.useUpdateRefIfShallowNew(decodedValuesCacheRef, decodedValues); | ||
helpers_1.useUpdateRefIfShallowNew(decodedValuesCacheRef, decodedValues, function (a, b) { | ||
return shallowEqual_1.shallowEqualMap(a, b, paramConfigMap); | ||
}); | ||
// create a setter for updating multiple query params at once | ||
@@ -94,0 +96,0 @@ var setQuery = React.useCallback(function (changes, updateType) { |
{ | ||
"name": "use-query-params", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "React Hook for managing state in URL query parameters with easy serialization.", | ||
@@ -82,3 +82,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"serialize-query-params": "^1.1.0" | ||
"serialize-query-params": "^1.2.0" | ||
}, | ||
@@ -85,0 +85,0 @@ "husky": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95782
1655