React Native - Listview triggering multiple refreshes when pulled down

As I attempt to implement the onScroll function for ListView to detect when a user has pulled the list down beyond a certain pixel value in order to trigger an AJAX call and refresh the list, I am facing an issue where it fires multiple times leading to a choppy animation of the Activity Indicator.

This seems to occur because the check

e.nativeEvent.contentOffset.y < -50
is being performed frequently during the pull down and release. Is there a more efficient way to implement pull down refresh in ListView or perhaps a better approach to managing the spinner? Thank you.

  _handleScrollView(e) {
    if (e.nativeEvent.contentOffset.y < -50 && !this.props.listingsLoading) {
      // updates listingsLoading to false upon completion
      this.props.fetchListings()
    }
  }

  _renderLoadingView() {
    if (this.props.listingsLoading) {
      return (
         <ActivityIndicatorIOS />
      );
    }
  }

  render() {
    var listings = this.state.dataSource

    return (
      <View>
        {this._renderLoadingView()}
        <ListView
          onScroll={this._handleScrollView}
          dataSource={listings}
          renderRow={this._renderRow} />
      </View>
    )
  }

EDIT: To resolve the issue, I have added the following code snippet to utilize RefreshControl feature, my thanks to David!

            <ListView
              refreshControl={
                <RefreshControl
                  refreshing={this.state.isRefreshing}
                  onRefresh={this._onRefresh}
                  tintColor="#ff0000"
                  colors={['#ff0000', '#00ff00', '#0000ff']}
                  progressBackgroundColor="#ffff00"
                />
              }
              dataSource={listings}
              renderRow={this._renderRow}/>

I also defined an onRefresh function as follows:

  _onRefresh() {
    this.setState({isRefreshing: true});
    this.props.fetchListings(this.props.nextPage)
    setTimeout(() => {
      this.setState({
        isRefreshing: false
      });
    }, 3000);
  }

Answer №1

Utilizing the integrated Refresh Control is recommended for implementing pull-to-refresh functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I link two separate webpages upon submitting a form with a single click?

Here is a snippet of my code: <form action="register.php" method="post"> <input type="text" name="uname"> <input type="submit" > </form> Within the register.php file, there are codes for connecting to a database. I am looking ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

Discovering the Essence of AngularJS Test Runner: Unraveling the

I recently started learning Angular JS and decided to follow the tutorial here. I've encountered a roadblock in step 8 where I need to write a test to check if the thumbnail images are being displayed. The concept behind it is simple. There is a JSON ...

Mastering the management of various events within React Material UI components

I am working with a Material UI Switch Component and need to detect click events on it. In certain situations, I want to prevent the change event from triggering. What is the most effective way to achieve this? While I have previously used event.preventD ...

When the mouse hovers over it, show text instead of an icon on a React Material UI button

I'm currently working on a project that involves using material ui buttons. Initially, the add button only displays the + icon. Now, I want to change the button content from the icon to the text "CREATE ITEM" when the mouse is hovered over it. Check ...

Reloading jQuery event handlers following successful ajax request

I have been working on implementing behavior for DOM objects that are added via AJAX. After reviewing the jQuery API Docs, I believe it would be most effective to incorporate a scoped onInit(scope) handler to handle event binding on the new content. Here ...

Include the image source in the array

Currently, I am utilizing this loop to showcase images using Smarty {foreach from=$gallery item=image key=KEY} <div class="col-lg-2 col-md-2 col-sm-4 col-xs-6 smallImage"> <img ng-click="bigImage('/files/galleries/{$image.gallery ...

In JavaScript, a nameless function is being returned within another nameless function

Can the getNameFun function just return this.name instead of an anonymous function? Is there a reason for this structure? Code Segment 1: var name = "The Window";   var object = {     name : "My Object",     getNameFunc : function(){ ...

Refreshing the input with jQuery when the URL hash changes

$( "ul#results" ).click(function() {$( this ).slideUp();}); In my current project, I have implemented the code found in a tutorial at . The jQuery code above is specifically intended for handling an AJAX dropdown on the page. My objective is to achieve a ...

When the menu active item is clicked, the header will automatically scroll to the top

I've implemented a sticky header using CSS and JavaScript along with a mega menu. However, I've encountered an issue where when scrolling to the bottom and clicking on the "more" link in the dropdown, the page scrolls back to the top. This is the ...

In the realm of Laravel, Vue, and Javascript, one may question: what is the best approach to omitting a key

When working with JSON data, I encountered a problem where leaving some keys unfilled resulted in incorrect results. I want to find a way to skip these keys if they are not filled. I have shared my code for both the backend and frontend below. Backend La ...

Another method to verify an input using HTML5 regex and JavaScript

When working with vanilla JavaScript to check an HTML input against a regex pattern, things can get tricky. I find myself going back to the parent element to validate the input, and while it works, it's not as elegant as I would like it to be. Here i ...

Having difficulty identifying the same element during testing

Here is the code snippet I'm working with, which checks for expected text: console.log(typeof browser.getText('.modal.modal--primary.pin-container h1')); expect(browser.getText('.modal.modal--primary.pin-container h1')).toContain( ...

What is the method for retrieving the XMLHttpRequest errors registered with addEventListener?

I am struggling to find a solution. https://i.stack.imgur.com/bRJho.gif ...

Utilizing a TypeScript function to trigger an action from within a Chart.js option callback

Currently, I am utilizing a wrapper for Chart.js that enables an animation callback to signify when the chart has finished drawing. The chart options in my code are set up like this: public chartOptions: any = { animation: { duration: 2000, ...

The table refuses to load

I've been troubleshooting this issue for the past two days. The array is visible in the console, but it refuses to show up on the table. I've tried multiple approaches, but none seem to work. I suspect that "tobodyHtml" is not defined properly, a ...

Manipulate images in real-time and insert custom text using JavaScript/jQuery

Within my possession is an image depicted above. My objective revolves around dynamically altering the values present at the occurrences of L, A, and B; to achieve this, I must eliminate or conceal L, A, and B while substituting them with numerical equiv ...

"Encountered an unexpected token when using an object parameter in a React function

I am facing an issue with a function I have implemented in React Native. const HandleConfirmApplication = async ( opts: { isInvite: boolean, confirmationCheckValues: () => any, confirmPopUp: () => any, loadingApply: () => any, ...

Click on a specific image in a table using Cypress with a specific source URL

I need help with a query for Cypress regarding a table of items, each item having active (blue) and not active (black) images. How can I set up this query? Below is an image of the table list: https://i.stack.imgur.com/74qzb.png And here is the HTML code ...

Shade the entire td column in different colors depending on the characteristics of th

I am working with a table and here is the code for it: <table> <thead> <tr> <th style="width: 40%; text-align: center; vertical-align: center;">Nr.<br> crt.</th> <th style="font-weight: bold; wi ...