Next.js Discord

Discord Forum

Cannot read properties of undefined in server component?

Unanswered
dperolio posted this in #help-forum
Open in Discord
So, I have a simple Heading client component:

'use client';

import classNames from 'classnames';

/**
 *
 */
const Levels = {
  ONE: 1,
  TWO: 2,
  THREE: 3,
  FOUR: 4,
  FIVE: 5,
  SIX: 6
};

function Heading ({
  level,
  looks,
  className,
  children,
  ...other
}: HeadingProps) {
  const HeadingLevel = `h${level}`;

  return (
    <HeadingLevel {...other} className={classNames('heading', className, { [`h${looks}`]: looks })}>
      {children}
    </HeadingLevel>
  );
}

Heading.Levels = Levels;

export default Heading;


Then in a page I have:

<Heading level={Heading.Levels.ONE}>
  Some Header Here
</Heading>
and I am receiving this error:
TypeError: Cannot read properties of undefined (reading 'ONE')
at Home (./src/app/page.tsx:16:80)
at stringify (<anonymous>)
digest: "2456780610"
7 | return (
8 | #Unknown Channel
> 9 | <Heading level={Heading.Levels.ONE}>

What am I doing wrong?

6 Replies

Bump.
This exact code is working fine in Astro.
If I add 'use client'; to the top of the page.tsx, it works (I know I don't want to do this).
Am I not understanding how to do this correctly?
Okay, I added 'use client'; to my components index export file. Got a new error:
Unhandled Runtime Error
Error: Cannot access Levels.ONE on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.
Why can't we do this? I'm confused.