javascript - 当我 setState() 时,FlatList 项目不更新

标签 javascript react-native react-native-flatlist

我正在构建多选模式。当用户按下该项目时,该项目应标记为“已选中”。

问题 我从 id 数组中添加/删除了 id。当我打开并检查模态时,它没有显示“检查”标志。但是当我关闭并再次打开模式时,它会显示“检查”标志。

enter image description here

为了跟踪所选项目,我在模态组件的状态中定义了项目。

  state = {
    selectedSeasonIds: this.props.selectedSeasonIds,
   }

这是我用来在屏幕上显示模态的 react-native-modal

<Modal
      isVisible={isSelectorVisible}
      onBackdropPress = {() => this.props.hideSelector()}>
      <View style={styles.modalContainer}>
          <FlatList
              style={styles.root}
              data={this.props.items}
              ItemSeparatorComponent={this._renderSeparator}
              keyExtractor={this._keyExtractor}
              renderItem={this._renderItemForMultiple}/>
      </View>
</Modal>

这是每个项目的渲染函数

_renderItemForMultiple = ({item}) => {
    return (
      <TouchableOpacity
        style={styles.itemStyle}
        onPress={() => {this._handleMultipleItemPress(item.id)}}>
        <RkText>{item.value}</RkText>
        { this._renderCheck(item.id) }   <<< Here is the problem
      </TouchableOpacity>
    );
  }

当用户点击项目时,FlatList 的项目调用 _handleMultipleitemPress

  _handleMultipleItemPress = (id) => {
    let { selectionType } = this.props;
    let { selectedSeasonIds, selectedSizeIds, selectedColorIds } = this.state;
    if(selectionType===2) {
      if(_.includes(this.state.selectedSeasonIds, id)) {
        let newSelectedSeasonIds = _.filter(this.state.selectedSeasonIds, (curObject) => {
            return curObject !== id;
        });
        this.setState({selectedSeasonIds : newSelectedSeasonIds});
      } else {
        let newSelectedSeasonIds = [...this.state.selectedSeasonIds, id];
        this.setState({selectedSeasonIds : newSelectedSeasonIds});
      }
    }
    // season Select Action
    this.props.seasonSelectAction(id);
  }

问题 我们在 id 数组中添加/删除了 id。当我打开并检查模态时,它没有显示“检查”标志。但是当我关闭并再次打开模式时,它会显示“检查”标志。

即使我们在 renderCheck()setState 也无法渲染模态。为什么会这样?我该如何解决?

  _renderCheck = (id) => {
    let { selectionType, selectedSeasonIds, selectedSizeIds, selectedColorIds } = this.props;
    if(selectionType===2) {
      if(_.includes(this.state.selectedSeasonIds, id)) {
        return (<RkText>Check </RkText>);
      }
    } 
    return (<RkText> </RkText>);
  }

任何其他建议也将不胜感激!感谢阅读这篇文章。

更新 我用代码进行了调试,当我按下该项目时,它没有通过 _renderItemForMultiple。我认为这是因为我没有为 _renderItemForMultiple 定义参数。如何将项目传递给它的参数?有什么想法吗?

最佳答案

即使您的状态发生变化,您也不会将其传递给 <FlatList> ,所以它的 Prop 不会改变。它的shouldComponentUpdate方法返回 false当没有它的 Prop 改变时。正如文档所述:

By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.

所以你需要通过extraData={this.state}FlatList .

关于javascript - 当我 setState() 时,FlatList 项目不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007546/

相关文章:

javascript - 如何知道 FlatList 的最后一项

reactjs - 如何通过React Native的平面列表中的onpress on view来减少和增加特定 View 的高度?

javascript - 将类添加到最近的 ul

javascript - 为什么这个 javascript 的权限被拒绝?

javascript - d3 tree <g> 标签 padding/border 整体样式

javascript - 从不同的 HTML 标签中获取多个对应的值以在一个函数中使用

android - Detox .tap() 函数只突出显示,不按下。为什么?

react-native - 修改react-native-chart-kit中标签的宽度

ios - FBSDK 和 LinkingManager 的 AppDelegate.m

react-native - initialScrollIndex 不适用于 FlatList react 原生