oliviertassinari / react-swipeable-views

A React component for swipeable views. :snowflake:
https://react-swipeable-views.com/
MIT License
4.45k stars 470 forks source link

Lazy loading mounting all children #533

Open dmitrika opened 4 years ago

dmitrika commented 4 years ago

Hey guys :wave:

As a result of changes in https://github.com/oliviertassinari/react-swipeable-views/issues/453 right now after mounting all children are rendered.

My use case: I use react-swipeable-views on SERP for a listing preview gallery, each gallery has 5 pictures. So after mount, we might end up with Nx5 images to be loaded, even if only N of them would be visible.

I would like to propose API change to be able to provide a prop to change a number of slides to be rendered in advance:

// Example of propTypes and defaultProps
SwipeableViews.propTypes = {
  ...propTypes,
  /**
   * This is the number of rendered in advance childs if lazy loading is on
   */
  renderedInAdvance: PropTypes.number, 
}

SwipeableViews.defaultProps = {
  ...defaultProps,
    renderedInAdvance: 1, 
}

// Example of test case
it('should render in advance number of children according to prop', () => {
    const wrapper = mount(
        <SwipeableViews index={0} renderedInAdvance={2} >
          <div>{'slide n°1'}</div>
          <div>{'slide n°2'}</div>
          <div>{'slide n°3'}</div>
          <div>{'slide n°4'}</div>
        </SwipeableViews>,
    );

    assert.strictEqual(wrapper.text(), 'slide n°1');
    clock.tick(1);
    wrapper.update();
    assert.strictEqual(wrapper.text(), 'slide n°1slide n°2slide n°3');
});

Something like this... I'm not sure if it's aligned with initial ideas, I'm ready to implement this, first want to verify with repo maintainers.

Expected Behavior

I was expecting lazy loading to prevent over-fetching via rendering only active element (means downloading the only active picture), or at least active + 1.

Current Behavior

On mount we render all children.

Steps to Reproduce (for bugs)

Check out default code-sandbox example to verify that all children rendered. https://codesandbox.io/s/x8x11zjmp

Screenshot 2019-08-09 at 10 01 30

Test case: https://github.com/oliviertassinari/react-swipeable-views/blob/master/packages/react-swipeable-views/src/SwipeableViews.test.js#L614

Context

As far as I understood it was done to enable heavy views to be rendered in advance. But for me that looks like we completely removed "lazy-loading" feature, we just skip rendering on the server. In case of images, all of them would be loaded on the client.

Your Environment

Tech Version
react-swipeable-views last version
React last version
platform all platforms
Rc85 commented 3 years ago

Is the Virtualize HOC what you're looking for? Upon inspecting the elements in the demo, it looks like it renders/mounts the previous 2 and the 2 after. You can also increase the number slides to render/mount using overscan.

I haven't tried it though.