[Feature] granular forceUpdate #9218

thysultan avatar
thysultan commented on Mar 19, 2017

When forceUpdate is called on a long list at the very least the whole list must be traversed. For example with the following list arr = [1, 2, 3, 4, 5, 6] and if we where to push 7 arr.push(7) the reconciler will go through elements 1 - 6 and then add a 7 at the end of that list. The suggestion to add support for the following forceUpdate signature.

forceUpdate(a: {function|number}, b: number?)

Where when passed as an integer a is the start index and b the number of items to traverse similar to how str.substr() works.

When the function encounters a number as the a argument it changes the start index of where to start when reconciling the components children and when the b argument is set it changes the end index of where to end the traversal of the components children. So with the above mentioned example if i wanted to optimize the reconciliation process for append updates i could do the following.

this.forceUpdate(arr.length);

and to target an update in the middle of the list of up to 3 items.

this.forceUpdate(arr.length/2, 3);

This will allow for very precise O(1) updates when you know ahead of time how the data will change.

Possible setState counter-part.

setState(a: {function|Object}, b: (function|number)?, c: {number})

// usage
setState({arr: [...]}, this.state.arr.length);