Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel error, using Astro: useCarousel must be used within a <Carousel /> #2890

Closed
trespaul opened this issue Mar 2, 2024 · 2 comments
Closed

Comments

@trespaul
Copy link

trespaul commented Mar 2, 2024

On a fresh installation following the Astro installation instructions, using the Carousel component gives the useCarousel must be used within a <Carousel /> error.

Other components such as Button and Card are working.

index.astro:

import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from '@/components/ui/carousel'
---
<!doctype html>
<html lang="en">
  <body>
    <Carousel client:load>
      <CarouselContent>
        <CarouselItem><div>Slide 1</div></CarouselItem>
        <CarouselItem><div>Slide 2</div></CarouselItem>
        <CarouselItem><div>Slide 3</div></CarouselItem>
      </CarouselContent>
      <CarouselPrevious />
      <CarouselNext />
    </Carousel>
  </body>
</html>

Stack trace:

Error: useCarousel must be used within a <Carousel />
    at useCarousel (/home/paul/tmp/test/src/components/ui/carousel.tsx:37:11)
    at /home/paul/tmp/test/src/components/ui/carousel.tsx:199:54
    at renderWithHooks (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:5662:16)
    at renderForwardRef (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:5846:18)
    at renderElement (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6009:11)
    at renderNodeDestructiveImpl (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6108:11)
    at renderNodeDestructive (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6080:14)
    at retryTask (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6532:5)
    at performWork (/home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6580:7)
    at /home/paul/tmp/test/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:6904:12

I have tried using the <Carousel>...</Carousel> element in a React wrapper component; I have tried adding client:load to each child component; using client:only gives the exact same error but in the browser console instead.

Possibly a bug?

This is the only reference I could find to this error anywhere, and the proposed solution did not work for me, and I also don't see why the above shouldn't work.

@ImPrankster
Copy link

I tried in a new astro project and it works for me, have you abstracted the whole Carousel to a react component?
Like

<Layout title="Welcome to Astro.">
  <main>
    <InteractiveCarousel client:load />
  </main>
</Layout>

in interactiveCarousel:

import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@/components/ui/carousel";
import { Card, CardContent } from "@/components/ui/card";

const InteractiveCarousel = () => {
  return (
    <Carousel className="w-full max-w-xs">
      <CarouselContent>
        {Array.from({ length: 5 }).map((_, index) => (
          <CarouselItem key={index}>
            <div className="p-1">
              <Card>
                <CardContent className="flex aspect-square items-center justify-center p-6">
                  <span className="text-4xl font-semibold">{index + 1}</span>
                </CardContent>
              </Card>
            </div>
          </CarouselItem>
        ))}
      </CarouselContent>
      <CarouselPrevious />
      <CarouselNext />
    </Carousel>
  );
};

export default InteractiveCarousel;

Since astro can't handle children from other UI framework as-is. Button & Card works fine cause (I assume) all the children are plain text?

@trespaul
Copy link
Author

trespaul commented Mar 3, 2024

Oh. I was under the impression that it would be fine since it's in one island. And I probably also didn't want to accept that it would be necessary to create a wrapper component for any shadcn components that have subcomponents. 🤦

Thanks for the help and the link to the doc, I missed that section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants