FormidableLabs / nuka-carousel

Small, fast, and accessibility-first React carousel library with an easily customizable UI and behavior to fit your brand and site.
https://commerce.nearform.com/open-source/nuka-carousel
Other
2.99k stars 594 forks source link

[8.0.0] RangeError: invalid array length #1053

Closed Silvenga closed 2 months ago

Silvenga commented 2 months ago

Is there an existing issue for this?

Code of Conduct

Code Sandbox link

No response

Bug report

After upgrading to 8.0.0 (and fixing breaking changes) loading the page now throws a "RangeError: invalid array length" on page load caused by a divide by zero.

The exception is coming from this code:

https://github.com/FormidableLabs/nuka-carousel/blob/b8edcae5ebd135fe5f29a28a7c772235b5310074/packages/nuka/src/hooks/use-measurement.tsx#L11-L36

The argument pageCount is set to Infinity, which coming from a divide by 0 as visibleWidth is 0.

This is mostly a race-condition during initialization.

The full stack trace (manually created as I have a dev hook intercepting the stack trace):

RangeError: invalid array length
arraySeq, node_modules/nuka-carousel/src/utils/array.ts:8:9
useMeasurement, node_modules/nuka-carousel/src/hooks/use-measurement.tsx:33:24
commitHookEffectListMount, node_modules/react-dom/cjs/react-dom.development.js:23150:25
... a bunch of React internals during mount.
Silvenga commented 2 months ago

I would guess handling the divide by zero would be enough here, if visibleWidth === 0, then pageCount should be set to 0.