简体   繁体   English

反应本机FlatList项目不随状态变化

[英]React native FlatList items not changing with state

I have a search bar that updates this.state.data which feeds my FlatList. 我有一个更新this.state.data的搜索栏,它可以输入我的FlatList。 When I type something like tttt in the SearchBar..... this.state.data is updated to an empty array....and the console.log you see below console.log('Populating flatview with:' + this.state.data); 当我在SearchBar中键入tttt之类的内容时,.. this.state.data将更新为空数组console.log('Populating flatview with:' + this.state.data); shows that the array is now emptied...however the FlatList does not change to reflect this. 显示数组现在已清空...但是FlatList不会更改以反映这一点。 this.state.data is definitely changing to an empty array but the FlatList is not refreshing. this.state.data肯定会更改为空数组,但FlatList不会刷新。

Any ideas? 有任何想法吗? Thanks in advance 提前致谢

  constructor(props) {
    super(props);

    this.state = {
      data: [],
    };
    this.arrayholder = [];
  }

this.state.data and this.arrayholder get populated with objects here..... this.state.data和this.arrayholder在此处填充对象。

 <SearchBar
    placeholder="Type Here..."
    lightTheme
    round
    onChangeText={text => this.searchFilterFunction(text)}
    autoCorrect={false}
  />

  searchFilterFunction = (text) => {
    const newData = this.arrayholder.filter((item) => {
      const itemData = item.name.toUpperCase();
      const textData = text.toUpperCase();

      return itemData.indexOf(textData) > -1;
    });
    this.setState({ data: newData });
  };



renderFlatView = () => {
    console.log('Populating flatview with:' + this.state.data);
    return (
      <List containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0 }}>
        <FlatList
          data={this.state.data}
          renderItem={({ item }) => (
            <ListItem
              roundAvatar
              title={item.title}
              subtitle={item.subtitle}
              avatar={item.avatar}
              containerStyle={{ borderBottomWidth: 0 }}
              onPress={function goes here}
            />
          )}
          keyExtractor={item => item.id}
          ItemSeparatorComponent={this.renderSeparator}
          ListHeaderComponent={this.renderHeader}
          ListFooterComponent={this.renderFooter}
          onRefresh={this._handleRefresh}
          refreshing={this.state.refreshing}
          onEndReached={this._handleLoadMore}
          onEndReachedThreshold={0.3}
        />
      </List>
    );
  };

...figured it out. ...弄清楚了。

this._handleLoadMore was firing immediately after state was filtered...and fetched a bunch more results from the API to add to state this._handleLoadMore在过滤状态后立即触发...并从API获取了更多结果以添加到状态

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

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