MCPcopy Create free account
hub / github.com/Kureev/react-native-list / scrollToIndex

Function scrollToIndex

VirtualizedList.js:203–223  ·  view source on GitHub ↗
(params: {
    animated?: ?boolean, index: number, viewOffset?: number, viewPosition?: number
  })

Source from the content-addressed store, hash-verified

201
202 // scrollToIndex may be janky without getItemLayout prop
203 scrollToIndex(params: {
204 animated?: ?boolean, index: number, viewOffset?: number, viewPosition?: number
205 }) {
206 const {data, horizontal, getItemCount, getItemLayout} = this.props;
207 const {animated, index, viewOffset, viewPosition} = params;
208 invariant(
209 index >= 0 && index < getItemCount(data),
210 `scrollToIndex out of range: ${index} vs ${getItemCount(data) - 1}`,
211 );
212 invariant(
213 getItemLayout || index < this._highestMeasuredFrameIndex,
214 'scrollToIndex should be used in conjunction with getItemLayout, ' +
215 'otherwise there is no way to know the location of an arbitrary index.',
216 );
217 const frame = this._getFrameMetricsApprox(index);
218 const offset = Math.max(
219 0,
220 frame.offset - (viewPosition || 0) * (this._scrollMetrics.visibleLength - frame.length),
221 ) - (viewOffset || 0);
222 this._scrollRef.scrollTo(horizontal ? {x: offset, animated} : {y: offset, animated});
223 }
224
225 // scrollToItem may be janky without getItemLayout prop. Required linear scan through items -
226 // use scrollToIndex instead if possible.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected