Closed yaodingyd closed 4 years ago
@yaodingyd I can't get this to work locally, can you try with the example here https://github.com/theolampert/react-flickity-component-example ?
if you run something like: npm link && npm run prepublishOnly -- --watch
on your branch
I'll add a dev script in another PR soon
@theolampert updated. Could you try with both static
set and unset?
@yaodingyd I get an infinite render loop now, I think theres an issue in how we're using state to determine if the carousel has been created. Maybe it's better to use a static variable to set a flickityCreated
state? I will take a closer look.
I think I fix the infinite render. I added a check in setReady
to only trigger when it's not. The previous infinite render is because it keeps setting ready.
The setReady
is in a setTimeout call because createPortal is an async operation and DOM will be updated later, and I need to make sure when imagesloaded
is called, children is rendered in DOM.
I added a third state for dynamically adding/removing slides. Since setReady
would only be called when it's not, we need a way to set ready to false. I'm comparing number of cells for now.
@theolampert can you take a look?
@yaodingyd sorry haven't had a chance to propery test this. Will take a look tonight.
The issue is because before the
imagedloaded
code is incDM
life cycle, where the elementcarousel
used for watch for imageloaded actually doesn't have any images inside, because we append children in renderPortal function, which happens aftercDM
after a state change.The fix is to move this part of logic in
renderPortal
aftercarousel
element actually contains images so imageloaded can function properly. To triggerrenderPortal
we need a state change so I add another stateflickityCreated
, and set it to true when we create flickity.When
static
is set we don't use portal to render, but still we need to changeflickityReady
state.Currently version works in Chrome larger breakpoints is somewhat lucky. I tried in small breakpoints and it doesn't work either. My wild guess is in these large breakpoints Chrome is doing some optimization on image loading so it actually work