Skip to content

Commit 1147e3f

Browse files
authored
Merge pull request #9 from space-fe/fix/pre-location
fix: update state pre location query
2 parents 6599d74 + 276cf62 commit 1147e3f

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

es/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ var queryToPropsHOC = function queryToPropsHOC(DecoratedComponent, config) {
321321
_this = _possibleConstructorReturn(this, _getPrototypeOf(queryToPropsComponent).call(this, props));
322322

323323
_defineProperty(_assertThisInitialized(_this), "__getLocationQueryObj", function (location) {
324-
var currLocation = location || _this.currLocation;
325-
return currLocation ? queryString.parse(currLocation.search, {
324+
var l = location || _this.currLocation;
325+
return l ? queryString.parse(l.search, {
326326
arrayFormat: 'comma'
327327
}) : {};
328328
});
@@ -334,10 +334,10 @@ var queryToPropsHOC = function queryToPropsHOC(DecoratedComponent, config) {
334334
});
335335

336336
_defineProperty(_assertThisInitialized(_this), "__getValidatedQueryObj", function (location) {
337-
var currentQueryObj = _this.__getLocationQueryObj(location);
337+
var queryObj = _this.__getLocationQueryObj(location);
338338

339339
var filterKeys = Object.keys(queryPropsConfig);
340-
var filterQueryObj = filterObjWithDefaultObj(currentQueryObj, defaultState, filterKeys);
340+
var filterQueryObj = filterObjWithDefaultObj(queryObj, defaultState, filterKeys);
341341
var decodedQueryObj = decodeObj(filterQueryObj, queryPropsConfig);
342342
var validatedQueryObj = validateObject(decodedQueryObj, defaultState, validatorMap);
343343
return validatedQueryObj;
@@ -358,10 +358,9 @@ var queryToPropsHOC = function queryToPropsHOC(DecoratedComponent, config) {
358358

359359
var validatedState = validateObject(newState, defaultState, validatorMap);
360360

361-
_this.__updateUrl(validatedState); // this.currLocation has not been changed at this time
361+
_this.__updateUrl(validatedState);
362362

363-
364-
var prevValidatedQueryObj = _this.__getValidatedQueryObj();
363+
var prevValidatedQueryObj = _this.__getValidatedQueryObj(_this.prevLocation);
365364

366365
if (!deepEqual(prevValidatedQueryObj, validatedState)) {
367366
_this.setState(_objectSpread({}, validatedState), function () {
@@ -371,6 +370,7 @@ var queryToPropsHOC = function queryToPropsHOC(DecoratedComponent, config) {
371370
});
372371

373372
_defineProperty(_assertThisInitialized(_this), "handleRouteChanged", function (prevLocation, currLocation) {
373+
_this.prevLocation = prevLocation;
374374
_this.currLocation = currLocation;
375375

376376
var validatedQueryObj = _this.__getValidatedQueryObj(currLocation);
@@ -385,6 +385,7 @@ var queryToPropsHOC = function queryToPropsHOC(DecoratedComponent, config) {
385385
});
386386

387387
_this.__firstCallHandleRouteChanged = false;
388+
_this.prevLocation = null;
388389
_this.currLocation = null;
389390

390391
var _validatedQueryObj = _this.__getValidatedQueryObj(props.location);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-query-string-to-props",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "Mapping query string from url to Component props seamlessly.",
55
"main": "es/index.js",
66
"module": "es/index.js",

src/QueryToPropsHOC.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ const queryToPropsHOC = (DecoratedComponent, config) => {
3636
constructor (props) {
3737
super(props)
3838
this.__firstCallHandleRouteChanged = false
39+
this.prevLocation = null
3940
this.currLocation = null
4041

4142
const validatedQueryObj = this.__getValidatedQueryObj(props.location)
4243
this.state = { ...defaultState, ...validatedQueryObj }
4344
}
4445

4546
__getLocationQueryObj = (location) => {
46-
const currLocation = location || this.currLocation
47+
const l = location || this.currLocation
4748

48-
return currLocation
49-
? queryString.parse(currLocation.search, { arrayFormat: 'comma' })
49+
return l
50+
? queryString.parse(l.search, { arrayFormat: 'comma' })
5051
: {}
5152
}
5253

@@ -55,10 +56,10 @@ const queryToPropsHOC = (DecoratedComponent, config) => {
5556
}
5657

5758
__getValidatedQueryObj = (location) => {
58-
const currentQueryObj = this.__getLocationQueryObj(location)
59+
const queryObj = this.__getLocationQueryObj(location)
5960

6061
const filterKeys = Object.keys(queryPropsConfig)
61-
const filterQueryObj = filterObjWithDefaultObj(currentQueryObj, defaultState, filterKeys)
62+
const filterQueryObj = filterObjWithDefaultObj(queryObj, defaultState, filterKeys)
6263

6364
const decodedQueryObj = decodeObj(filterQueryObj, queryPropsConfig)
6465
const validatedQueryObj = validateObject(decodedQueryObj, defaultState, validatorMap)
@@ -88,8 +89,7 @@ const queryToPropsHOC = (DecoratedComponent, config) => {
8889
const validatedState = validateObject(newState, defaultState, validatorMap)
8990
this.__updateUrl(validatedState)
9091

91-
// this.currLocation has not been changed at this time
92-
const prevValidatedQueryObj = this.__getValidatedQueryObj()
92+
const prevValidatedQueryObj = this.__getValidatedQueryObj(this.prevLocation)
9393

9494
if (!deepEqual(prevValidatedQueryObj, validatedState)) {
9595
this.setState({ ...validatedState }, () => {
@@ -99,6 +99,7 @@ const queryToPropsHOC = (DecoratedComponent, config) => {
9999
}
100100

101101
handleRouteChanged = (prevLocation, currLocation) => {
102+
this.prevLocation = prevLocation
102103
this.currLocation = currLocation
103104

104105
const validatedQueryObj = this.__getValidatedQueryObj(currLocation)

types/index.d.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ interface IValidator {
99
value: ValidatorValue
1010
}
1111

12-
declare function queryToPropsHOC<T>(
13-
DecoratedComponent: React.ComponentType,
14-
config: {
15-
history: History
16-
queryPropsConfig: {
17-
[key: string]: QueryPropTypes
18-
}
19-
defaultQueryProps?: {
20-
[key: string]: PropValue
21-
},
22-
validatorMap?: {
23-
[key: string]: IValidator[]
24-
}
25-
replaceRouteWhenChange?: boolean
26-
mapDefaultQueryPropsToUrlWhenMounted?: boolean
27-
}
28-
): React.ComponentType<T>
12+
interface IMap<T> {
13+
[key: string]: T
14+
}
15+
16+
interface IConfig {
17+
history: History
18+
queryPropsConfig: IMap<QueryPropTypes>
19+
defaultQueryProps?: IMap<PropValue>
20+
validatorMap?: IMap<IValidator[]>
21+
replaceRouteWhenChange?: boolean
22+
mapDefaultQueryPropsToUrlWhenMounted?: boolean
23+
}
24+
25+
declare function queryToPropsHOC<T>(DecoratedComponent: React.ComponentType, config: IConfig): React.ComponentType<T>
2926

3027
declare enum QueryPropTypes {
3128
number = 'number',
@@ -41,6 +38,6 @@ declare enum ValidateTypes {
4138
function = 'function'
4239
}
4340

44-
export { QueryPropTypes, ValidateTypes }
41+
export { QueryPropTypes, ValidateTypes, IConfig }
4542

4643
export default queryToPropsHOC

0 commit comments

Comments
 (0)