The virtual positioning of list items in React Virtualized

Using React Virtualized in a setup similar to the snippet provided below has raised an issue. A few items are being rendered using a CellMeasurer. The last item, which indicates that more items will be loaded, is not rendering correctly. It has the wrong position/style and height associated with it. How can this issue be resolved?

If you would like to experiment with it, here is a link to a Plunkr: https://plnkr.co/edit/TrKdNu4FfNsXqERPVnYo?p=preview

var List = ReactVirtualized.List;
var CellMeasurer = ReactVirtualized.CellMeasurer;
var CellMeasurerCache = ReactVirtualized.CellMeasurerCache;

var cache = new CellMeasurerCache({
  fixedWidth: true,
  minHeight: 50
});

// List data represented as an array of strings
var namesList = [
  'Brian Vaughn',
  'Bob Smith',
  'Someone Else',
  'I hate making up names for the sake of names.'
  // More names...
];

var App = React.createClass({
  getInitialState: function() {
    return {
      list: namesList
    };
  },
  render: function() {
    return <List
    className='List'
    width={300}
    height={300}
    rowCount={this.state.list.length + 1}
    rowHeight={cache.rowHeight}
    deferredMeasurementCache={cache}
    list={this.state.list}
    rowRenderer={
      ({ index, isScrolling, key, parent, style }) => {
        var item = this.state.list[index];
        let result;
        if (!item) {
          console.log(index);
          result =
            <div className='Row'
          style={style}
          key={key}
            >Loading!!!
          </div>; }
        else
          result = <CellMeasurer
        cache={cache}
        columnIndex={0}
        key={key}
        style={style}
        rowIndex={index}
        parent={parent}>
          {

              <div
            className='Row'
            key={key}
            style={style}
              >
              {this.state.list[index]}
            </div>
          }
        </CellMeasurer>;
        return result;
      }}/>;
  }
});

// Render your list
ReactDOM.render(
  <App/>,
  document.getElementById('example')
);
.List {
  border: 1px solid black;
  box-sizing: border-box;
}
.Row {
  width: 100%;
  height: 100%;
  border-bottom: 1px solid black;
  display: flex;
  align-items: center;
  padding: 0 0.5rem;
  box-sizing: border-box;
}
<!DOCTYPE html>
<html>
  <head>
    <script src="https://unpkg.com/react/dist/react-with-addons.min.js"></script>
    <script src="https://unpkg.com/react-dom/dist/react-dom.min.js"></script>
    <script src="https://unpkg.com/react-virtualized/dist/umd/react-virtualized.js"></script>

    <link rel="stylesheet" href="https://unpkg.com/react-virtualized/styles.css">
    <link rel="stylesheet" href="styles.css">
  </head>

  <body>
    <div id="example">
      <!-- This element's contents will be replaced with your code... -->
    </div>
    <script src="script.js" defer></script>
  </body>

</html>

Answer №1

CellMeasurer was not designed to be used selectively for certain rows in this manner. However, I can see why it may seem like it should work that way from an external perspective.

When Grid renders a cell and detects the use of CellMeasurer, it positions the cell at top:0, left:0 to allow it room to expand within the container, despite being constrained by its size. This behavior is somewhat puzzling, but that's how it currently operates.

In your situation, Grid assumes the last row needs to be measured, places it at 0,0, and expects it to expand accordingly.

To resolve this issue for now, you could simply wrap that row in CellMeasurer as well.

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

Regular expression that prohibits the acceptance of numbers with leading zeros

Below is the directive I am using to ensure that the input consists purely of numbers from 0-9. import { Directive, HostListener, ElementRef } from "@angular/core"; @Directive({ selector: "[numbersOnly]", }) export class OnlynumberDi ...

Identifying button clicks using selenium-wire in Python

Is there a way to use selenium-wire in python to capture a full screenshot of a web page and save it as a png file after clicking the submit button? Despite seeing a popup message saying "taking screenshot!!", the actual screenshot file is not being saved ...

Searching for a specific value within a list that has been fetched from a database using AngularJs can be achieved by simply clicking on the search button

Seeking assistance on a search functionality issue. I am able to filter search results from a list retrieved from the database and displayed on an HTML page, but facing difficulty in getting complete search results from the controller. When clicking the se ...

Summing Up Values in Jquery Loop Through Table Rows

I am facing a challenge with a table that contains a textfield for inputting numeric values. This textfield is part of a repeated row table, with the ID 'amount'. My goal is to calculate the sum of all values entered in the textfields with the ID ...

jQuery includes inArray as the final element in an array or object

There's a strange issue I've noticed where, in some cases, when jQuery is imported, it appends the inArray function as the last element of a defined object or array. This can cause problems with for ... in loops since it counts this unexpected fu ...

The function is missing from the object, leading to a script error with jQuery

When using different versions of jQuery, I encountered some issues with saving changes in my application. Initially, with jquery-1.4.4.min.js, everything worked except for the save function due to an error I made. However, when switching to jquery-1.7.1.mi ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...

Require assistance with try-catch statements

I am troubleshooting an issue with a try-catch block in my Protractor test. Take a look at the code snippet below: try { element(by.id('usernameas')).sendKeys(data); } catch(err) { console.log('error occurred'); } To test the ...

Find and filter the values in the range.getValues() array that correspond to the first element in apps script

In the spreadsheet provided, I have compiled a list of cities in different states of my country. This information will be utilized in a form; however, it is not feasible for users to sift through an extensive list of all cities listed. Therefore, I am look ...

What is the best method for saving a chosen radio button into an array?

I am currently developing an online examination system where questions are retrieved from a database using PHP and displayed through AJAX. I am facing an issue where I am unable to capture the selected radio button value and store it in an array. Despite e ...

Is it possible to link an asp.net dropdownlist to javascript/jquery?

I'm looking to bind an asp.net dropdownlist using javascript/jquery. I am retrieving the data from a web method using jquery ajax, so I want to avoid postback at this point. However, I still need to be able to do postback and save all the data using s ...

Opening a new window with Node-Webkit's start function

My application built on node-webkit has a control window and a separate presentation window. The control window collects data and triggers the opening of the presentation window using the window.open function. Once the presentation window is open, it can ...

Is there a replacement for jQuery's each() function in vanilla JavaScript?

When working with jQuery, we often use the each function in our code like the example below: $('button').each(function(i) { $('button').eq(i).css('background', 'red'); }); Is there a way to achieve similar functi ...

In Angular, there is a situation where two input fields are both referencing the same event.target

I am facing an issue where I have two input fields that are linked to the same event.target.value object, but I want them to be separate. <form class="flex-list" [formGroup]="calculation_Input" (input)="input($eve ...

Utilize SCSS values within TypeScript by applying them on a class level

let changeClassDisplay = document.getElementsByClassName('sidebar'); for (var i = 0; i < changeClassDisplay.length; i += 1) { changeClassDisplay[i].style.display = 'block'; } I encountered an issue with this code whe ...

Verifying the status following a promise execution in the initial promise function

Below is the function I am currently working with: startGame = () => { this.buildDeck() .then(this.shuffleDeck) .then(this.dealToPlayer) .then(setTimeout(this.dealToPlayer, 2000)) .then(setTimeout(this.dealToDealer, 4000)) } In ...

Ways to retrieve the href attribute value from an element in Python/Selenium even when there is no explicit href attribute present

I am facing an issue with retrieving the URL link from an element using the get_attribute('href') method. It returns a null value, as if the element does not have a href attribute. webdriver.find_element_by_xpath('//*[@id="__next"] ...

Creating a shared singleton instance in Typescript that can be accessed by multiple modules

Within my typescript application, there is a Database class set up as a singleton to ensure only one instance exists: export default class Database { private static instance: Database; //Actual class logic removed public static getInstance() ...

A guide to showcasing a map on a web browser with Python and the DJANGO API

I am facing an issue with displaying my map on a web browser. I am utilizing the DJANGO API and attempting to showcase the map (which is written in Python) in the form of a table using HTML. views.py def default_map(request): world = folium.Map( ...

Using jQuery to toggle sliding the information above a div

I am facing an issue with my customized sliding menu. The menu slides over the image but not over the content-div, pushing it aside. I have tried to fix this problem but haven't found a solution yet. My goal is for the menu to slide over all divs and ...