Skip to content

Commit 6540f08

Browse files
committed
Remove side-effects from renders
1 parent 14b99a5 commit 6540f08

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/SortableList.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default class SortableList extends Component {
4747
/**
4848
* Stores promises of rows’ layouts.
4949
*/
50-
_rowsLayouts = [];
50+
_rowsLayouts = {};
51+
_resolveRowLayout = {};
5152

5253
_contentOffset = {x: 0, y: 0};
5354

@@ -64,23 +65,43 @@ export default class SortableList extends Component {
6465
scrollEnabled: this.props.scrollEnabled
6566
};
6667

68+
componentWillMount() {
69+
this.state.order.forEach((key) => {
70+
this._rowsLayouts[key] = new Promise((resolve) => {
71+
this._resolveRowLayout[key] = resolve;
72+
});
73+
});
74+
75+
if (this.props.renderFooter && !this.props.horizontal) {
76+
this._footerLayout = new Promise((resolve) => {
77+
this._resolveFooterLayout = resolve;
78+
});
79+
}
80+
}
81+
6782
componentDidMount() {
6883
this._onUpdateLayouts();
6984
}
7085

7186
componentWillReceiveProps(nextProps) {
7287
const {data, order} = this.state;
73-
const {data: nextData, order: nextOrder} = nextProps;
88+
let {data: nextData, order: nextOrder} = nextProps;
7489

7590
if (data && nextData && !shallowEqual(data, nextData)) {
91+
nextOrder = nextOrder || Object.keys(nextData)
7692
uniqueRowKey.id++;
77-
this._rowsLayouts = [];
93+
this._rowsLayouts = {};
94+
nextOrder.forEach((key) => {
95+
this._rowsLayouts[key] = new Promise((resolve) => {
96+
this._resolveRowLayout[key] = resolve;
97+
});
98+
});
7899
this.setState({
79100
animated: false,
80101
data: nextData,
81102
containerLayout: null,
82103
rowsLayouts: null,
83-
order: nextOrder || Object.keys(nextData)
104+
order: nextOrder
84105
});
85106

86107
} else if (order && nextOrder && !shallowEqual(order, nextOrder)) {
@@ -206,7 +227,6 @@ export default class SortableList extends Component {
206227
return order.map((key, index) => {
207228
const style = {[ZINDEX]: 0};
208229
const location = {x: 0, y: 0};
209-
let resolveLayout;
210230

211231
if (rowsLayouts) {
212232
if (horizontal) {
@@ -218,8 +238,6 @@ export default class SortableList extends Component {
218238
location.y = nextY;
219239
nextY += rowsLayouts[key].height;
220240
}
221-
} else {
222-
this._rowsLayouts.push(new Promise((resolve) => (resolveLayout = resolve)));
223241
}
224242

225243
const active = activeRowKey === key;
@@ -239,7 +257,7 @@ export default class SortableList extends Component {
239257
disabled={!sortingEnabled}
240258
style={style}
241259
location={location}
242-
onLayout={!rowsLayouts ? this._onLayoutRow.bind(this, resolveLayout, key) : null}
260+
onLayout={!rowsLayouts ? this._onLayoutRow.bind(this, key) : null}
243261
onActivate={this._onActivateRow.bind(this, key, index)}
244262
onPress={this._onPressRow.bind(this, key)}
245263
onRelease={this._onReleaseRow.bind(this, key)}
@@ -262,21 +280,16 @@ export default class SortableList extends Component {
262280
}
263281

264282
const {footerLayout} = this.state;
265-
let resolveLayout;
266-
267-
if (!footerLayout) {
268-
this._footerLayout = new Promise((resolve) => (resolveLayout = resolve));
269-
}
270283

271284
return (
272-
<View onLayout={!footerLayout ? this._onLayoutFooter.bind(this, resolveLayout) : null}>
285+
<View onLayout={!footerLayout ? this._onLayoutFooter : null}>
273286
{this.props.renderFooter()}
274287
</View>
275288
);
276289
}
277290

278291
_onUpdateLayouts() {
279-
Promise.all([this._footerLayout, ...this._rowsLayouts])
292+
Promise.all([this._footerLayout, ...Object.values(this._rowsLayouts)])
280293
.then(([footerLayout, ...rowsLayouts]) => {
281294
// Can get correct container’s layout only after rows’s layouts.
282295
this._container.measure((x, y, width, height, pageX, pageY) => {
@@ -515,13 +528,13 @@ export default class SortableList extends Component {
515528
this._autoScrollInterval = null;
516529
}
517530

518-
_onLayoutRow(resolveLayout, rowKey, {nativeEvent: {layout}}) {
519-
resolveLayout({rowKey, layout});
531+
_onLayoutRow(rowKey, {nativeEvent: {layout}}) {
532+
this._resolveRowLayout[rowKey]({rowKey, layout});
520533
}
521534

522-
_onLayoutFooter(resolveLayout, {nativeEvent: {layout}}) {
523-
resolveLayout(layout);
524-
}
535+
_onLayoutFooter = ({nativeEvent: {layout}}) => {
536+
this._resolveFooterLayout(layout);
537+
};
525538

526539
_onActivateRow = (rowKey, index, e, gestureState, location) => {
527540
this._activeRowLocation = location;

0 commit comments

Comments
 (0)