question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calculating height of item

See original GitHub issue

This is probably related to #70; I still have an issue with calculating heights of my items using data, I used this approach where I tried to use getDataForIndex but nothing rendered on the screen:

this._layoutProvider = new LayoutProvider(
  index => {
    return "DEFAULT";
  },
  (type, dim, index) => {
    const item = this._dataProvider.getDataForIndex(index);
    if (item.media) {
      if (item.media.length > 0) {
        if (item.media.length === 1) {
          dim.width = TabletWidth;
          dim.height = 106 + (TabletWidth * (item.media.height / item.media.width));
        } else if (item.media.length === 2) {
          dim.width = TabletWidth;
          dim.height = 106 + (((TabletWidth - 20) / 2) + 8);
        } else if (item.media.length === 3) {
          dim.width = TabletWidth;
          dim.height = 106 + (((TabletWidth - 20) / 2) + (TabletWidth / 2.2) + 8);
        } else {
          dim.width = TabletWidth;
          dim.height = 106 + ((((TabletWidth - 20) / 2) + 8) * 2);
        }
      }
    }
  });

then I used this approach and it didn’t work like before:

this._layoutProvider = new LayoutProvider(
  index => {
    return "DEFAULT";
  },
  (type, dim, item) => {
    if (item.media) {
      if (item.media.length > 0) {
        if (item.media.length === 1) {
          dim.width = TabletWidth;
          dim.height = 106 + (TabletWidth * (item.media.height / item.media.width));
        }
        else if (item.media.length === 2) {
          dim.width = TabletWidth;
          dim.height = 106 + (((TabletWidth - 20) / 2) + 8);
        }
        else if (item.media.length === 3) {
          dim.width = TabletWidth;
          dim.height = 106 + (((TabletWidth - 20) / 2) + (TabletWidth / 2.2) + 8);
        }
        else {
          dim.width = TabletWidth;
          dim.height = 106 + ((((TabletWidth - 20) / 2) + 8) * 2);
        }
      }
    }
  });

I also tested this:

this._layoutProvider = new LayoutProvider(()=>{return "DEFAULT"}, (data, dim)=>{
  dim.width = TabletWidth;
  dim.height = 106 + (
    data.media ?
    data.media.length > 0 ?
    data.media.length === 1 ?
    TabletWidth * (data.media.height / data.media.width)
    : data.media.length === 2 ?
    ((TabletWidth - 20) / 2) + 8
    : data.media.length === 3 ?
    ((TabletWidth - 20) / 2) + (TabletWidth / 2.2) + 8
    :
    (((TabletWidth - 20) / 2) + 8) * 2
    : null : null
  )
});

this code only calculates 106 as the height of all items! and ignores all the conditions!

am I missing something here, how can I calculate item height based on item data?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
naqvitalhacommented, Feb 6, 2018

Few things I found wrong with the code:

  1. Estimated height was 1 and it needs to be an actual estimate for optimal performance.
  2. Instead of having one type called “DEFAULT” have multiple ones backed by their own components. Right now the problem is that you have one big component which renders separate stuff based on conditions why now let rowRenderer choose a component instead. You have some components with just text and if you use the same to render an image the text component will get unmounted ideally you must avoid unnecessary unmounts and promote reuse. That is the point of recycling.
  3. You have lot of component level setState which I don’t think are necessary, remember they will slow things down. Also, changing things like imageLoaded1 will not re-render a component being reused incase the previous use case also had the same value. That’s how recycling works.

More that than you are getting dimensions upfront in your data source. I see thing being too complicated. Can you just try rendering a use case where only one photo is being shown? It’ll be easier for me to go through and for you to understand. Just render images, from my side I can assure you that this is a solved use case and we’ve done it before.

Your page can be much better. Reduce setState calls and break into components and add more types to LayoutProvider

1reaction
naqvitalhacommented, Feb 6, 2018

@Komeyl94 For some reason snack is not working for me. I checked multiple devices. I can look into the repo, no problem. Happy to help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to calculate the height of a div with some text inside and a ...
There isn't really a way to calculate height of an element that is not in the dom. You can add it somewhere in...
Read more >
How to Calculate Height - Sciencing
Calculate the height of the object of interest by calculating "D * tan (theta)," where "*" indicates multiplication and "tan" is the tangent ......
Read more >
Length, Width & Height to Volume Calculator - SensorsONE
Use this length x width x height calculator to determine the volume in the following applications: Volume of package to be dispatched to...
Read more >
How to Calculate the Image Height of an Object for a Lens
The equation for the magnification of a lens is simply the height of the image divided by the height of the object. M=h ......
Read more >
Height and Distance Calculation with Formula & Example
The calculation of the height of an object is achieved by the measurement of its distance from the object. This includes the angle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found