Skip to content

Importing my custom component in a Next 14 project #1

@raphox

Description

@raphox

When I am trying to import a component from my custom library in a Next 14 project, I am getting the following exception:

Screenshot 2024-02-05 at 07 36 16

My current code is:

// pages/index.js
import * as React from "react";

import { Separator } from "metacomet-ui-components";

import Layout from "@/components/layout";

export default function StatementsPage() {
  return (
    <Layout>
      <Separator />
    </Layout>
  );
}

// separator.tsx
import * as React from "react";
import * as SeparatorPrimitive from "@radix-ui/react-separator";

import { cn } from "@/lib/utils";

const Separator = React.forwardRef<
  React.ElementRef<typeof SeparatorPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof SeparatorPrimitive.Root>
>(
  (
    { className, orientation = "horizontal", decorative = true, ...props },
    ref
  ) => (
    <SeparatorPrimitive.Root
      ref={ref}
      decorative={decorative}
      orientation={orientation}
      className={cn(
        "shrink-0 bg-border",
        orientation === "horizontal" ? "h-[1px] w-full" : "h-full w-[1px]",
        className
      )}
      {...props}
    />
  )
);
Separator.displayName = SeparatorPrimitive.Root.displayName;

export { Separator };

My custom component extends the https://ui.shadcn.com/ and the problem is related to the uses of the method React.useEffect.

The message printed in the console is:

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

My collected infos:

  1. I have more than one copy of React in the same app. Checking the build JS files in the browser console, I can see two definitions of the React library in different files. I am using React and ReacDom as peerDependencies to prevent this, but it seems to be not working.
  2. After reading many posts, I tried to use the use client notation to fix it without success.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions