react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

Problem in rendering Slider with dynamic children

Open chochihim opened this issue 10 years ago • 44 comments

If I render Slider as below:

<Slider {...settings}>
    {slides.map((slide) => (
        <div key={slide.index}>{slide.index}</div>
    ))}
</Slider>

nextProps.children.length in componentWillReceiveProps of class InnerSlider will always returns 1 which breaks the sliding logic.

Does Slider not expect dynamic children?

chochihim avatar Oct 03 '16 07:10 chochihim

Can you reproduce the issue with this jsfiddle and provide link https://jsfiddle.net/kirana/20bumb4g/

akiran avatar Oct 03 '16 08:10 akiran

Sure. Here's the implementation of my described problem: https://jsfiddle.net/8qLhy7do/1/

If options slidesToShow and slidesToScroll are set to 1, it works fine: https://jsfiddle.net/8qLhy7do/2/

chochihim avatar Oct 03 '16 11:10 chochihim

Hey chochihim, it looks like you're not returning your <div> inside map()

try:

<Slider {...settings}>
    {slides.map((slide) => (
        return (<div key={slide.index}>{slide.index}</div>);
    ))}
</Slider>

walston avatar Oct 04 '16 22:10 walston

@walston no, mine is valid shorthand

In my jsfiddle example, the initial rendering is fine but not after clicking 'Add slide'.

chochihim avatar Oct 05 '16 02:10 chochihim

+1 Having this issue too. On version 0.14.5 for react-slick.

anonmily avatar Oct 13 '16 13:10 anonmily

+1

roik7 avatar Oct 13 '16 16:10 roik7

version 15.3.2 Cannot read property 'getBoundingClientRect' of null

zhujunel avatar Oct 14 '16 09:10 zhujunel

+1 same here. basically this line fails return elem.getBoundingClientRect().height || elem.offsetHeight; because elem is null. It comes from slickList.querySelector('[data-index="0"]').

hackhat avatar Oct 21 '16 14:10 hackhat

@hackhat I had exactly the same issue and fixed by adding data-index attribute to my slide elements, more or less like this:

<Slider {...settings}>
  {slides.map((slide, index) => (
    <div data-index={index} key={index}>{index}</div>
  ))}
</Slider>

I've just started adding this library to my project, so I'm not sure if it won't cause some other issues later on.

szimek avatar Oct 26 '16 15:10 szimek

@szimek That's the same thing I did, but something doesn't feel right about it.

hackhat avatar Oct 30 '16 17:10 hackhat

@hackhat you can try this: <Slider {...settings}> <div> {slides.map((slide, index) => ( <div key={index}>{index}</div> ))} </div> </Slider>

ShuaiShuaiguo avatar Oct 31 '16 03:10 ShuaiShuaiguo

@ShuaiShuaiguo You mean removing the data-index={index} from the example above (see below code)? I don't think will work, because react-slick needs the data-index attribute.

<Slider {...settings}>
  {slides.map((slide, index) => (
    <div data-index={index} key={index}>{index}</div>
  ))}
</Slider>

hackhat avatar Nov 01 '16 09:11 hackhat

Which version of react-slick are you using ?

I don't see this issue with latest release. Check this demo https://jsfiddle.net/8qLhy7do/6/

akiran avatar Nov 01 '16 10:11 akiran

@akiran I got the issue with "version": "0.14.5"

hackhat avatar Nov 01 '16 14:11 hackhat

@hackhat Can provide a jsfiddle to replicate your issue ?

akiran avatar Nov 01 '16 15:11 akiran

@akiran I was able to reproduce the issue here:

https://jsfiddle.net/17xt8pxw/2/

It seems like if we start out with no children (e.g. this.state.slides = []), then there is a problem. When there is at least one children before the initialization of the slick slider (e.g. this.state.slides = [1]), then it still works.

Edit: Also, with further investigation, seems like the slick-track element remains empty.

anonmily avatar Nov 02 '16 07:11 anonmily

@hackhat no, it needs a certain DOM element, just wraps the dynamic data by any DOM elements, not one of element.it works to me.

ShuaiShuaiguo avatar Nov 03 '16 02:11 ShuaiShuaiguo

+1 This is definitely a bug with 14.5. I pretty much have the same set up as the OP but utilizing a Redux store. My store initializes as empty then is populated later. React-slick shouldn't even be initializing with an empty array.

samplefrequency avatar Nov 03 '16 15:11 samplefrequency

@samplefrequency Agreed. I will try to fix it soon

akiran avatar Nov 03 '16 17:11 akiran

+1, setting manually data-index solved the problem.

andricicezar avatar Nov 08 '16 08:11 andricicezar