Skip to content

Commit 57798d6

Browse files
authored
Merge pull request #948 from minran/master
fix: table don't change when this.state change data
2 parents afbfe7a + afd8c9f commit 57798d6

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

src/table/TableStore.jsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import type {
1010
Column,
1111
_Column
1212
} from './Types';
13-
import {
14-
deepCompare
15-
} from './utils'
1613

1714
import normalizeColumns from './normalizeColumns';
1815
import { getLeafColumns, getValueByPath, getColumns, convertToRows, getRowIdentity } from "./utils";
@@ -128,7 +125,7 @@ export default class TableStore extends Component<TableStoreProps, TableStoreSta
128125
if (getColumns(this.props) !== nextColumns) {
129126
this.updateColumns(nextColumns);
130127
}
131-
if (deepCompare(data,nextProps.data)){
128+
if (data !== nextProps.data) {
132129
this.updateData(nextProps);
133130
}
134131
}

src/table/utils.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,32 +133,30 @@ export function convertToRows(columns: Array<_Column>): Array<Array<_Column>> {
133133
return rows;
134134
}
135135

136-
const isObject = (obj: Object): boolean => {
137-
return Object.prototype.toString.call(obj) === '[object Object]'
138-
}
139-
const isArray = (arr: number): boolean => {
140-
return Object.prototype.toString.call(arr) === '[object Array]'
141-
}
136+
const checkType = (data: any): string => Object.prototype.toString.call(data).toLowerCase().slice(8, -1);
137+
142138
const deepCompare = (obj1: any, obj2: any): boolean => {
143-
if (obj1 && obj2 && obj1.length !== obj2.length) {
144-
return true
145-
} else if (isArray(obj1) && isArray(obj2)) {
146-
return obj1.some((value, key) => (
147-
deepCompare(value, obj2[key])
148-
))
149-
} else if (isObject(obj1) && isObject(obj2)) {
150-
for (let key in obj1) {
151-
if (deepCompare(obj1[key], obj2[key])) {
152-
return true
153-
}
139+
const obj1Type = checkType(obj1);
140+
const obj2Type = checkType(obj2);
141+
if (obj1Type !== obj2Type ) return false;
142+
143+
if (obj1Type === 'array' && obj1.length === obj2.length) {
144+
return obj1.every((value, key) => (
145+
deepCompare(value, obj2[key])
146+
))
147+
}
148+
149+
if (obj1Type === 'object') {
150+
for (let key in obj1) {
151+
if (!Object.keys(obj2).includes(key)) return false;
152+
return deepCompare(obj1[key], obj2[key])
153+
}
154+
return false
154155
}
155-
return false
156-
}
157-
return obj1 !== obj2
156+
return Object.is(obj1,obj2);
158157
}
159158

160159
export {
161160
deepCompare,
162-
isObject,
163-
isArray
161+
checkType,
164162
}

0 commit comments

Comments
 (0)