ui icon indicating copy to clipboard operation
ui copied to clipboard

Error with React Context Creation in @radix-ui/react-dismissable-layer

Open thewbuk opened this issue 1 year ago • 5 comments

I'm encountering a runtime error in my Next.js application involving the @radix-ui/react-dismissable-layer package. The application fails with the following error message:

Error: (0 , react__WEBPACK_IMPORTED_MODULE_1__.createContext) is not a function

Code :


                        <Dialog>
                          <DialogTrigger asChild>
                            <Button size="sm" className="relative" disabled>
                              Add channel
                            </Button>
                          </DialogTrigger>
                          <DialogContent>
                            <DialogHeader>
                              <DialogTitle>Add Podcast</DialogTitle>
                              <DialogDescription>
                                Copy and paste the podcast feed URL to import.
                              </DialogDescription>
                            </DialogHeader>
                            <div className="grid gap-4 py-4">
                              <div className="grid gap-2">
                                <Label>Podcast URL</Label>
                                <Input
                                  id="url"
                                  placeholder="https://example.com/feed.xml"
                                />
                              </div>
                            </div>
                            <DialogFooter>
                              <Button>Import Podcast</Button>
                            </DialogFooter>
                          </DialogContent>
                        </Dialog>

Full error message:

Server Error

Error: (0 , react__WEBPACK_IMPORTED_MODULE_1__.createContext) is not a function
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
eval
node_modules\@radix-ui\react-dismissable-layer\dist\index.mjs (23:89)
(rsc)/./node_modules/@radix-ui/react-dismissable-layer/dist/index.mjs

"@radix-ui/react-dismissable-layer": "^1.0.5",

thewbuk avatar Dec 03 '23 14:12 thewbuk

After some investigating i discovered that this is due to inconsistent imports and some packages auto imported from @radix-ui/react-dialog instead of @/components/ui/...

Not working:

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogTitle,
  DialogTrigger,
} from '@radix-ui/react-dialog';
import { DialogFooter, DialogHeader } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from 'recharts';

Working:

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';

Ideally, it should not matter, so I'll leave the issue open to have a discussion if this is feasible to implement. But I'll agree more of a user error on this one

thewbuk avatar Dec 03 '23 15:12 thewbuk

Error: (0 , react__WEBPACK_IMPORTED_MODULE_1__.createContext) is not a function

getting same error but for @/components/ui/accordion"

pratik2e avatar Dec 03 '23 22:12 pratik2e

@thewbuk

The reason for the error is that the Radix primitives for the dialog component utilize hooks but lack the necessary use-client directives at the top of the file. Consequently, when using the dialog in server components, this results in the mentioned error.

However, if you are implementing the dialog in a client component, it should work seamlessly. It's worth noting that for consistent styling, it is advisable to opt for shadcn/ui components instead of Radix primitives.

rajput-hemant avatar Dec 04 '23 19:12 rajput-hemant

Try adding "use client" to the top of each shadcn component file.

F-PTS avatar Dec 08 '23 16:12 F-PTS

Try adding "use client" to the top of each shadcn component file.

thanks mate it work

indigoscipio avatar Dec 27 '23 03:12 indigoscipio

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Feb 11 '24 23:02 shadcn