简体   繁体   English

React-Native (FlatList):如何知道可见项的渲染何时完成

[英]React-Native (FlatList): How to know when the rendering of visible items has finished

When using a FlatList Component in react native I need to know when all the visible items have been rendered.在 React Native 中使用 FlatList 组件时,我需要知道所有可见项何时都已呈现。

I am providing data and the renderItem when I get the componentDidMount I can see the FlatList there but because FlatList Component asynchronously renders each item they only show up after on componentDidUpdate .我提供datarenderItem时,我得到了componentDidMount我可以看到FlatList有,但因为FlatList组件异步呈现每个项目他们只后显示componentDidUpdate Also, this could (and probably will) include off-view items.此外,这可能(并且可能会)包括非视图项目。

I would like to know if there is someone out there that has found a possible way to have a controlled process (not with setTimeout ) of knowing when the visible items are fully rendered.我想知道是否有人找到了一种可能的方法来控制过程(不是使用setTimeout )来了解何时完全呈现可见项目。

Thanks.谢谢。

I ended up using the componentDidMount of the renderItem component as an indicator that element is rendered.我结束了使用的componentDidMount所述的renderItem组分作为该元素被呈现的指示符。

In the below example ActivityIndicator will be shown until the first 10 items are rendered (the number I set in the initialNumToRender ).在下面的示例中, ActivityIndicator将一直显示,直到呈现前 10 个项目(我在initialNumToRender设置的initialNumToRender )。

This approach is not ideal.这种方法并不理想。 You may need to tweak it for your case but the general idea is shown below.您可能需要针对您的情况对其进行调整,但总体思路如下所示。

Here is the list item:这是列表项:

class ListItem extends React.Component<{ onRendered: () => void }> {

  componentDidMount(): void {
    this.props.onRendered();
  }

  render() {
    return (
      <View>
        <Text>Item</Text>
      </View>
    );
  }
}

And here is the screen with FlatList that uses above ListItem:这是使用上面 ListItem 的带有FlatList的屏幕:

export class FlatListTestScreen extends React.Component<any, { creating: boolean }> {
  constructor(props: any, context: any) {
    super(props, context);
    this.state = {
      creating: true,
    };
  }

  onRenderedItem = () => {
    this.setState({
      creating: false,
    });
  };

  renderItem = (item: any) => {
    return <ListItem onRendered={this.onRenderedItem} />;
  };

  dataArray = Array(20)
    .fill('')
    .map((_, i) => ({ key: `${i}`, text: `item #${i}` }));

  render() {
    const loader = this.state.creating ? <ActivityIndicator /> : <></>;
    return (
      <View>
        {loader}
        <FlatList
          initialNumToRender={10}
          data={ this.dataArray }
          renderItem={ this.renderItem }
          keyExtractor={ item => item.key }
        />
      </View>
    );
  }
}

I also tried to use a map to collect the number of rendered items but on practice it seems that they all fire componentDidMount at the same time so simpler approach may be better.我还尝试使用地图来收集渲染项目的数量,但在实践中似乎它们都同时触发componentDidMount ,因此更简单的方法可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM