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

slick-slide calculating width wrong

Open lightninglu10 opened this issue 9 years ago • 25 comments

Hey guys, react-slick is calculating the width of the slick-slide incorrectly. It's inside of a container with 1190px and I have slidesToShow: 5, and it's calculating the width as 268px per card when it should be 238px. Not sure what's going on here.

I'm on a screen size of 1440px and there's 50px of padding on slick-list so it looks like it's almost trying to get the full screen size?

Issue can be seen here: https://lunyr.github.io/Lunyr-Alpha/

lightninglu10 avatar Jul 21 '17 19:07 lightninglu10

update: it calculates the width wrong initially, but then if I resize the browser, the elements are the correct width.

Any idea what's going on?

lightninglu10 avatar Jul 22 '17 01:07 lightninglu10

Found the issue. I'm using an external library for styling called Aphrodite. It seems like it doesn't render the aphrodite CSS quickly enough for react-slick to pick up on the initial width.

So I set an initial width inline and now it works fine. This seems like a bug with react-slick.

lightninglu10 avatar Jul 22 '17 08:07 lightninglu10

or just call resize before the render) but yes,its seems like a bug

KuriatoV avatar Jul 23 '17 15:07 KuriatoV

How do you call resize before rendering?

On Jul 23, 2017, at 8:51 AM, Valentyn Kuriato [email protected] wrote:

or just call resize before the render) but yes,its seems like a bug

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

lightninglu10 avatar Jul 23 '17 16:07 lightninglu10

maybe not exactly before,but straight after in componentDidMount by using ref this.refs.[your ref name].onWindowResize()

KuriatoV avatar Jul 23 '17 17:07 KuriatoV

I actually encountered exactly the same issue few hours ago ;-)

The answer of KuriatoV helped me a lot. Thanks to him ... You'll find a more precise answer below.

You just have to make sure that you pass down a prop to the slider in order to trigger the "componentWillReceiveProps" function

class Slider extends React.PureComponent {

 componentWillReceiveProps(){
   this.refs.slick.innerSlider.onWindowResized()
 }

 render () {

   var settings = {
     dots: false,
     infinite: true,
     speed: 100,
     slidesToShow: 1,
     slidesToScroll: 1,
     fade: true,
     lazyLoad: true,
     nextArrow: <NextArrow />,
     prevArrow: <PreviousArrow />,
   };
   return (
     <Slick ref="slick" {...settings}>
       {this.props.pictures.map((picture, index)=> <div key={index}> <Img picture={picture}></Img> </div>)}
     </Slick>
   );
 }
}

jchapelle avatar Jul 23 '17 19:07 jchapelle

nice one :)

KuriatoV avatar Jul 23 '17 22:07 KuriatoV

The solution here didn't quite work, but I was able to have the slider properly re-render using this method here in #299

via @omritoptix componentDidMount() { setTimeout(() => { window.dispatchEvent(new Event('resize')) }, 0); }

iamcam avatar Aug 29 '17 21:08 iamcam

I had this issue when using a "scale" css animation (in a modal) in Chrome. It calculates the initial size based on the pre-animation scale size. In a window resize event, it calculates the width of each item in the slider based on the currently scaled size during the animation.

mikepii avatar Sep 25 '17 16:09 mikepii

i'm facing the same (or related) issue, the slides are bigger than they should be but a window resize make them recalculate and display properly.

sag1v avatar Oct 09 '17 06:10 sag1v

I'm facing this problem right now, the slide's width is not calculated until resize. I try many approaches above but no hope :/

kv0906 avatar Oct 27 '17 10:10 kv0906

@kv0906 I'm using the componentDidMount life cycle method to dispatch the resize event but I'm doing it with setTimeout of 150 ms. you may need to play with the ms to get it right. it's a hack but that's the only thing working for me at the moment.

    componentDidMount() {
        setTimeout(() => { window.dispatchEvent(new Event('resize')); }, 150);
    }

sag1v avatar Oct 30 '17 06:10 sag1v

I am having the same issue ,non of the above solutions is working for me.

a-eid avatar Nov 03 '17 20:11 a-eid

My solution, for those that don't want to rely on setTimeouts or put the slider in another component:

componentDidMount() {

    $('img').on('load', ()=>{
      this.refs.slick.innerSlider.onWindowResized()
    })

  }

Note: you still have to have a ref to slick like in @jchapelle 's answer above

zachfeldman avatar Nov 09 '17 21:11 zachfeldman

Issue is still actual. Updated my project to React v16 and react-slick to v0.15.4. Carousel now looks bad after dynamic changing of items because of incorrect width calculating. After emulating window resize event everything works fine.

componentDidUpdate() {
    window.dispatchEvent(new Event('resize'));
}

mbielkin avatar Nov 16 '17 08:11 mbielkin

Best solution for me was using onLoad attribute on my img tags inside the slider.

<Slider>
  {images.map((image, index) => (
    <div key={index}>
      <img
        src={image.large_url}
        onLoad={() => window.dispatchEvent(new Event('resize'))}
      />
    </div>
  ))}
</Slider>

mmakarios avatar Nov 16 '17 16:11 mmakarios

The answer from @mmakarios is by far the best! Thanks!

zeckdude avatar Nov 27 '17 01:11 zeckdude

check the meta viewport tag in your html file, it should be <meta name="viewport" content="width=device-width, initial-scale=1">

MetaLic avatar Dec 18 '17 17:12 MetaLic

https://github.com/akiran/react-slick/commit/8591dace983fa1bc103964459ab99eaa34eed8de solves the problem. Changes will be released soon.

laveesingh avatar Mar 22 '18 08:03 laveesingh

Very cool! Thank you @laveesingh

zachfeldman avatar Mar 22 '18 14:03 zachfeldman