Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何通过更改任何状态值来重新呈现平面列表?_Reactjs_React Native_State - Fatal编程技术网

Reactjs 如何通过更改任何状态值来重新呈现平面列表?

Reactjs 如何通过更改任何状态值来重新呈现平面列表?,reactjs,react-native,state,Reactjs,React Native,State,我有一个状态 constructor (props) { super (props); this.state = { index: '', }; } 和平面列表 <FlatList showsVerticalScrollIndicator={false} removeClippedSubviews={false} data={this.props.response}

我有一个状态

constructor (props) {
    super (props);
    this.state = {
      index: '',
      };
  }
和平面列表

 <FlatList
          showsVerticalScrollIndicator={false}
          removeClippedSubviews={false}
          data={this.props.response}
          keyExtractor={(item, index) => index}
          renderItem={({item, index}) => this.renderFlatListItem (item, index)}
        />
我发现,通过设置索引值,ui正在呈现,但在renderFlatListItem内部没有呈现,因为this.props.response中没有更改


我正在寻找一种解决方案,如果任何其他状态发生变化,我希望重新呈现平面列表视图。让我知道是否可以使用react native?

平面列表是一个纯组件,因此当其道具没有变化时不会重新呈现。根据
平面列表
文档,通过传递
外部数据={this.state}可以导致重新呈现
平面列表

如果不设置此道具,FlatList将不知道需要重新渲染任何项目,因为它也是一个PureComponent,道具比较不会显示任何更改。同时,设置相同的状态不会导致它重新渲染

 <FlatList 
      extraData={this.state.index}
      showsVerticalScrollIndicator={false}
      removeClippedSubviews={false}
      data={this.props.response}
      keyExtractor={(item, index) => index}
      renderItem={({item, index}) => this.renderFlatListItem (item, index)}
    />

为什么在没有数据更改的情况下需要重新渲染列表?我正在设置列表项的颜色,这取决于索引。它会在列表外部更新。如果您确实总是想强制重新渲染,您可以给
FlatList
一个
属性加上递增的值,例如当前的unix时间戳。
 <FlatList 
      extraData={this.state.index}
      showsVerticalScrollIndicator={false}
      removeClippedSubviews={false}
      data={this.props.response}
      keyExtractor={(item, index) => index}
      renderItem={({item, index}) => this.renderFlatListItem (item, index)}
    />
this.setState(prevState => ({index: prevState.index + 1}));