简体   繁体   English

React Native:带有ExtraData的FlatList过滤器不会刷新

[英]React Native: FlatList filters with extraData won’t refresh

I have a Flatlist which works like a To Do list with a filter for “ToDo” and “Upcoming”. 我有一个Flatlist,它的工作方式类似于待办事项列表,带有用于“待办事项”和“即将到来”的过滤器。 When a user swipes to complete the item, it gets hidden from the list by changing a displayIndex attribute. 当用户滑动完成项目时,通过更改displayIndex属性将其从列表中隐藏。 I would like this to reload the list after the swipe or before the user selects “Upcoming”. 我希望在刷卡后或用户选择“即将到来”之前重新加载列表。 After reading through other stack overflow answers I have tried adding extraData={this.state} (and creating a this.state.refresh property which changes after every swipe) to the Flatlist and I also ensured that the list items themselves are React.Components and not PureComponents. 阅读完其他堆栈溢出的答案后,我尝试将FlatData = {this.state}(并创建this.state.refresh属性,每次滑动后都会更改)添加到Flatlist,并且我还确保列表项本身就是React.Components而不是PureComponents。 I have also tried two ways to hide the ListItems, conditionally rendering them and conditionally changing the style to hidden. 我还尝试了两种方法来隐藏ListItem,有条件地渲染它们以及有条件地将样式更改为隐藏。 Still, I am not seeing any change in my Flatlist. 不过,我的单位列表没有任何变化。

Below is some partial code to see if there are any gotchas I missed: 下面是一些部分代码,看是否有我错过的陷阱:

In the MainScreen.js 在MainScreen.js中

async _addCompletion(myItem) {
    //Lots of business logic and after it's done the below code activates
    await AsyncStorage.setItem(myItem.key, JSON.stringify(myItem));
    await this._updateData();
    this.setState({ refresh: !this.state.refresh });
}
 render() {
    const buttons = ['To Do', 'Upcoming'];
    const { displayModeIndex } = this.state;
    return (
      <View>
        <ButtonGroup
          onPress={this._updateButtonIndex}
          buttons={buttons}
          selectedIndex={displayModeIndex}
        />
        <FlatList
          displayMode={this.state.displayModeIndex}
          data={this.state.data}
          extraData={this.state}
          scrollEnabled={this.state.scrollEnabled}
          renderItem={({ item }) => (
            <MyListItem
              myListItem={item}
              addCompletion={this._addCompletion}
              displayIndex={this.state.displayModeIndex}
              setScrollEnabled={this._setScrollEnabled}
              navigation={this.props.navigation}
            />
          )}
        />
      </View>
    );
  }

In MyListItem.js 在MyListItem.js中

_displayMyItem {
   //Logic that determines whether to display a myItem based on several factors. I can confirm this works after refreshing.
}

  _hideMyItem = () => {
    Animated.timing(this.containerHeight, {
      toValue: 0,
    }).start(() => {
      this.setState({ hidden: true });
    });
  };

render () {
const {myItem} = this.state;
//Other code that determines how the list item looks depending on myItem data.
return (
//I have also tried to return null if this._displayMyItem(this.state.myItem) returns false 
<View style={!this._displayMyItem(this.state.myItem) && { display: 'none' }}>
          <Swipeable
            onPress={this._onPressRow}
            setScrollEnabled={this.props.setScrollEnabled}
            addCompletion={this.props.addCompletion}
            hideMyItem={this._hideMyItem}
            myItem={this.state.myItem}
          >
        //Other JSX Code
</View>
)
}

The Swipeable is a custom component that calls addCompletion after a swipe and _hideMyItem after everything is done. Swipeable是一个自定义组件,在完成滑动后调用addCompletion,在完成所有操作后调用_hideMyItem。 It is not a PureComponent either. 它也不是PureComponent。

There's a lot going on here, so I've only included code that seems relevant. 这里发生了很多事情,所以我只包含了似乎相关的代码。 I can add more if needed. 如果需要,我可以添加更多。 The addCompletion method is a long addCompletion方法很长

would help some captures... When you swipe the item , it's just empty right?, if it leaves an empty space try this way of conditional rendering , idk if it would work. 可以帮助捕获某些内容...当您滑动项目时,它只是空的吧?如果它留有空白,请尝试这种有条件的渲染方式,如果可行,请使用idk。

in MyListItem.js 在MyListItem.js中

 render () {
    const {myItem} = this.state;
    //Other code that determines how the list item looks depending on myItem data.
    return (
    //I have also tried to return null if this._displayMyItem(this.state.myItem) returns false 
{!this.state.hidden?
    <View style={!this._displayMyItem(this.state.myItem) && { display: 'none' }}>
              <Swipeable
                onPress={this._onPressRow}
                setScrollEnabled={this.props.setScrollEnabled}
                addCompletion={this.props.addCompletion}
                hideMyItem={this._hideMyItem}
                myItem={this.state.myItem}
              >
            //Other JSX Code
    </View>:null}
    )
    }

wich checks if this.state.hidden is false , returns the component, else, returns null wich检查this.state.hidden是否为false,返回组件,否则返回null

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

相关问题 React Native - 无法理解 FlatList extraData 道具的目的 - React Native - Can't understand the purpose of FlatList extraData prop 为什么我在 React-Native 中的 FlatList 没有更新,即使添加了额外数据? - Why my FlatList in React-Native doesn't update, even after adding extradata? 删除最后一项后无法重新渲染FlatList,尽管使用extraData-React Native - Can't Re-render FlatList after deleting last item Although use extraData - React Native 如何更新功能组件中的平面列表 arrays [extraData 不起作用] - How to update flatlist arrays in functional component [extraData won't work] 当使用extraData时,React Native FlatList会渲染两次数据 - React Native FlatList renders data twice when using extraData 在我滚动之前,React Native FlatList将不会呈现 - React Native FlatList won't render until I scroll Flatlist不会显示来自json的图像(React Native) - Flatlist won't show images from json (React Native) React Native FlatList 不会滚动到 header 组件的末尾 - React Native FlatList won't scroll to the end of the header component React Native,如何用FlatList实现pull to refresh? - React Native, how to implement pull to refresh with FlatList? 更改时,FlatList 中的 ExtraData 不会更新列表 - ExtraData in FlatList wont't update list when changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM