You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_getRangeToRender(): [number,number,number,number]{const{ itemCount, overscanCount }=this.props;const{ isScrolling, scrollDirection, scrollOffset }=this.state;if(itemCount===0){return[0,0,0,0];}// 该方法是使用时才从父组件传下来的conststartIndex=getStartIndexForOffset(this.props,scrollOffset,this._instanceProps);// 该方法是使用时才从父组件传下来的conststopIndex=getStopIndexForStartIndex(this.props,startIndex,scrollOffset,this._instanceProps);// Overscan by one item in each direction so that tab/focus works.// If there isn't at least one extra item, tab loops back around.constoverscanBackward=!isScrolling||scrollDirection==='backward'
? Math.max(1,overscanCount)
: 1;constoverscanForward=!isScrolling||scrollDirection==='forward'
? Math.max(1,overscanCount)
: 1;return[Math.max(0,startIndex-overscanBackward),// 往前加一个Math.max(0,Math.min(itemCount-1,stopIndex+overscanForward)),// 往后再加一个startIndex,stopIndex,];}
getStartIndexForOffset: ({ itemCount, itemSize }: Props<any>,
offset: number
): number =>Math.max(0,// 简单的将移动的个数算出来Math.min(itemCount-1,Math.floor(offset/((itemSize: any): number)))),
_onScrollHorizontal=(event: ScrollEvent): void=>{const{ clientWidth, scrollLeft, scrollWidth }=event.currentTarget;this.setState(prevState=>{if(prevState.scrollOffset===scrollLeft){// 如果移动距离是一样的就不更新了returnnull;}const{ direction }=this.props;letscrollOffset=scrollLeft;if(direction==='rtl'){constisNegative=isRTLOffsetNegative();// 这里是当向右阅读时scrollOffset的不同// TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.// This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).// It's also easier for this component if we convert offsets to the same format as they would be in for ltr.// So the simplest solution is to determine which browser behavior we're dealing with, and convert based on it.if(isNegative){scrollOffset=-scrollLeft;}else{scrollOffset=scrollWidth-clientWidth-scrollLeft;}}// Prevent Safari's elastic scrolling from causing visual shaking when scrolling past bounds.scrollOffset=Math.max(0,Math.min(scrollOffset,scrollWidth-clientWidth));return{isScrolling: true,scrollDirection:
prevState.scrollOffset<scrollLeft ? 'forward' : 'backward',
scrollOffset,scrollUpdateWasRequested: false,};},this._resetIsScrollingDebounced);};
The text was updated successfully, but these errors were encountered:
hulin32
changed the title
react-window 源码解析
react-window 源码解析--FixedSizeList
Jun 11, 2019
先看简单的的吧,FixedSizeList在底层调用了createListComponent, createListComponent是一个高阶函数,返回一个PureComponent
别怕,我看着也头晕,作者还使用了flow类型,我也没学过,但是感觉跟ts差不太多,还是可以看得懂。
然后我们根据声明周期来看看这个高阶函数吧,我就挑我认为重要的讲了.
在
getDerivedStateFromProps
会对参数做一些验证,因为其会在render方法前调用,放这里验证也比价合理然后在
render
里面,这里生成的
items
就是最终渲染在页面上的内容,这里巧的就是通过控制startIndex, stopIndex,只是渲染一部分的内容到页面上,所以即使有再多的数据,渲染的内容都是一样的,然后看看_getRangeToRender
方法然后重要的就是监听onScroll来改变scrollOffset的值了
当是垂直滚动时
当是水平滚动时
The text was updated successfully, but these errors were encountered: