Problem in rendering Slider with dynamic children
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?
Can you reproduce the issue with this jsfiddle and provide link https://jsfiddle.net/kirana/20bumb4g/
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/
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 no, mine is valid shorthand
In my jsfiddle example, the initial rendering is fine but not after clicking 'Add slide'.
+1 Having this issue too. On version 0.14.5 for react-slick.
+1
version 15.3.2 Cannot read property 'getBoundingClientRect' of null
+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 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 That's the same thing I did, but something doesn't feel right about it.
@hackhat you can try this: <Slider {...settings}> <div> {slides.map((slide, index) => ( <div key={index}>{index}</div> ))} </div> </Slider>
@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>
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 I got the issue with "version": "0.14.5"
@hackhat Can provide a jsfiddle to replicate your issue ?
@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.
@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.
+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 Agreed. I will try to fix it soon
+1, setting manually data-index solved the problem.