yaodingyd / react-flickity-component

A React.js component for using @desandro's Flickity
314 stars 51 forks source link

flickityRef not working. #113

Closed ryanlloydfrench closed 3 years ago

ryanlloydfrench commented 4 years ago

Hi,

My code is set up like the following however I keep getting Cannot read property 'flkty' of undefined

  function myCustomNext() {
    this.flkty.next();
  }
<Flickity flickityRef={c => this.flkty = c} ></Flickity>

How come I cannot access this? Is there a fix?

bartosjiri commented 3 years ago

You need to define the flkty variable first. At the moment, in your <Flickity> component you are trying to assign a value to an undefined/non-existing flkty variable.

In React v16.8+, you can achieve it by:

let flkty = null;

const myCustomNext = () => {
   flkty.next();
};
<Flickity flickityRef={c => (flkty = c)}>
...
</Flickity>