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.
@andricicezar Still won't fix the issue if the array starts off empty and is later populated via AJAX or from a Redux store.
Still not working :/
As a quick workaround, you can mount slider only if you have slides.
{slides.length > 0 ? <Slider>{slides}</Slider> : null }
@akiran I solve this problem almost like you:
{this.state.loading ? <Loader> : <Slider>{slides}</Slider> }
Thanks for react-slick!
@akiran {slides.length > 0 ? <Slider>{slides}</Slider> : null } didn't work for me.
Any updates?
Replace 'slides' with the name of the array that contains the slide items. For example, instead of slides.length use yourArray.length
On Nov 14, 2016 18:08, Pam [email protected] wrote:
@akiranhttps://github.com/akiran {slides.length > 0 ?
Any updates?
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/akiran/react-slick/issues/504#issuecomment-260515785, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AHrj26kslUdhM1D511Y5Em8b_K9x3dGrks5q-QYegaJpZM4KMS5h.
Thanks for the quick response @samplefrequency
Unfortunately it still doesn't work. Like you, my array starts off empty and is later populated via AJAX called after the Redux store has been updated (since it needs the new data from the redux store to make the call).
As you suggested, I have replaced it with my own array, and I've also confirmed the length of said array -
this.renderGallery(listing).length shows 7 when rendering it directly below my images.
The below is my renderGallery handler which I call when rendering
renderGallery() {
if (this.props.listingGalleryImages.data !== undefined) {
const galleryAssets = this.props.listingGalleryImages.data;
const images = galleryAssets.map((asset, index) => {
return (
<div data-index={index} key={index}><img key={asset.id} src={asset.url} /></div>
);
});
return images;
}
}
And inside the render:
<div className="container" >
{this.renderGallery(listing) !== undefined && this.renderGallery(listing).length > 0 ?
<Slider{...settings}>
<div>{this.renderGallery(listing)}</div>
</Slider> : null }
{this.renderGallery(listing) !== undefined ? this.renderGallery(listing).length : null}
</div>
I don't know if this is clear enough, happy to clarify further.
Btw, I'm using "react-slick": "^0.14.5",
and also have the style sheet included, since initially my Isomorphic / universal React app didn't work without.
https://github.com/akiran/react-slick/issues/273
Solved it.
Writing it here for ppl who run into this in the future :
Had help from a colleague and he pointed out that I needed only to take out the div from this line
<Slider{...settings}>
<div>{this.renderGallery(listing)}</div>
</Slider> : null }
Apparently, if I leave it there, the Slider will pick up that one big
Side note: I didn't even need the data-index in the .map, though the ternary is still needed for fallback purposes.
Code now looks like this:
{this.props.listingGalleryImages.loaded === true &&
<div className="container">
{this.renderGallery(listing).length > 0 ?
<Slider {...settings}>{this.renderGallery(listing) </Slider> : null }
</div>
}
@ypyang237 Just as a side note, instead of the ternary
this.renderGallery(listing).length > 0 ?
<Slider {...settings}>{this.renderGallery(listing) </Slider> : null
You could actually use && to make it shorter:
( this.renderGallery(listing).length > 0 ) && <Slider {...settings}>{this.renderGallery(listing) </Slider>
@szimek Thanks, this works for me! 馃憤
@anonmily thanks i have the same problem as you , it works!
If {...settings} you set lazyLoad: true will not work. with data.length < settings.slidesToShow !!!
Doesn't work with react components
I also stuck in this issue, but now it's working fine for me:
<Slider
ref={c => this.slider = c}
{...settings}
afterChange={this.changeState}>
{content.map((item, index) => <p key={index}>{item}</p>)}
</Slider>
I hope it helps.
This issue is fixed in master
When will this fix be added to NPM?
Still facing the same issue with this release --
https://github.com/akiran/react-slick/releases/tag/0.15.0
Make sure data-index is an integer. And if your slide is a component, make sure to set the data-index attribute to the root element of your slide component.
Your slide component could look something like this:
const Slide = ({ dataIndex }) => {
return(
<div data-index={dataIndex} key={dataIndex}>
<p>Slide {dataIndex}</p>
</div>
)
}
This fixed it for me.
How is it possible to do what @ypyang237 did first? I need a slider that has that setup
<Slider{...settings}>
<div><div className="inner">{this.renderGallery(listing)}</div></div>
</Slider> : null }
And slick only to pick up like .inner as slides. In original slick-slider there is the slide attribute, which tells slick from which selectors to create the slides. It seems not implemented in react-slick?
Hello I still have the same problem... what the hell is going on... look at the width size...
my code :
<div>
<h2>Custom Arrows</h2>
<Slider {...settings}>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
<div>
<h3>3</h3>
</div>
<div>
<h3>4</h3>
</div>
<div>
<h3>5</h3>
</div>
<div>
<h3>6</h3>
</div>
</Slider>
</div>

Most helpful comment
@samplefrequency Agreed. I will try to fix it soon