React-Native 平面列表下拉刷新未显示

Raj*_*epe 3 javascript react-native react-native-flatlist

我正在拉取 flatlist 并刷新标题。在执行时,微调器不会出现。请告诉我我必须解决什么问题。我们需要使用刷新控制吗?

环境

react-native-cli: 2.0.1

反应原生:0.52.0

节点:v8.9.4

class ListSwipe extends Component {

  constructor(props) {
    super(props);
    this.state = { keywords: "", isLoading: true , results:[] , oldresults:[] , isFetching: false }     
  }

  componentDidMount() { 
    return fetch('https://reactnativecode.000webhostapp.com/FruitsList.php')
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({
          isLoading: false,
          isFetching: false  ,
          results: responseJson,
          oldresults: responseJson
        },...

makeRemoteRequest() {
....
}

handleRefresh = () => {

  this.setState({ isFetching: true }, function() {
    this.makeRemoteRequest()
  });

}


 ....
              <ScrollView style={styles.scroll}>


        <Text> Keywords : {this.state.keywords} </Text>



               {this.state.loading ? (
                <Spinner />
          ) :  <FlatList 
                 extraData={this.state}
                data={this.state.results}
                keyExtractor={(item, index) => item.id}
                renderItem={( {item} ) => {
                    return <ListItem>

                      <Text>{item.fruit_name}</Text>

                  </ListItem>
                  }}  
                  refreshing = {this.state.isFetching}                
                  onRefresh ={this.handleRefresh}
                  onRefresh={() => this.onRefresh()}

         /> }

        </ScrollView> 
Run Code Online (Sandbox Code Playgroud)

Moh*_*ard 9

因为你在 ScrollView 中使用 FlatList 那么你应该为 ScrollView 定义刷新,像这样:

<ScrollView
      refreshControl={ 
      <RefreshControl 
      refreshing={this.state.isFetching} 
      onRefresh={this.handleRefresh} 
      /> 
      } 
 >
<FlatList ... />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

不要忘记导入 RefreshControl :

import { RefreshControl } from 'react-native';
Run Code Online (Sandbox Code Playgroud)

  • 你不应该将 Flatlist 放在 ScrollView 中。Flatlist 针对大数据进行了优化;它会在需要时渲染项目并释放 RAM。将其放在 ScrollView 下会消除所有优化。阅读此处:https://nyxo.app/fixing-virtualizedlists-should-never-be-nested-inside-plain-scrollviews (2认同)