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

Reference Error: document is not defined #45

Open
kimjisena opened this issue Oct 11, 2022 · 8 comments
Open

Reference Error: document is not defined #45

kimjisena opened this issue Oct 11, 2022 · 8 comments
Assignees
Labels

Comments

@kimjisena
Copy link
Contributor

Describe the bug
I'm trying to render a binary tree using react-organizational-chart but I keep getting a Reference Error: document is not defined.

To Reproduce
I have a BinaryTree component which I'm rendering in a network page

// components/BinaryTree.tsx

import { Tree, TreeNode } from "react-organizational-chart";

export default function BinaryTree () {
  return (
    <div>
      <Tree label={<div>Root</div>}>
        <TreeNode label={<div>Left</div>}>
          <TreeNode label={<div>Left</div>} />
          <TreeNode label={<div>Right</div>}/>
        </TreeNode>
        <TreeNode label={<div>Right</div>}>
          <TreeNode label={<div>Left</div>} />
          <TreeNode label={<div>Right</div>}/>
        </TreeNode>
      </Tree>
    </div>
  );
}
// pages/network.tsx

import SEOHead from "../components/SEOHead";
import Layout from "../components/Layout";
import BinaryTree from "../components/BinaryTree";

export default function Network () {

  return (
    <div className={`w-full scroll-smooth flex flex-row-reverse md:pr-[4%] lg:pr-[8%]`}>
      <SEOHead title="Network" />
      <Layout active={1}>
        <BinaryTree />
      </Layout>
    </div>
  );
}

Expected behavior
I expected to get a simple binary tree with 6 child nodes.

Screenshots
Here's a screenshot of the error logs from my build step:

tree-error

Browser (please complete the following information):

  • Browser: Mozilla Firefox
  • Version: 104.0.2

Additional context

  • I'm only getting this error when using NextJs.

  • I'm using

 "next": "^12.3.1",
 "react": "^18.2.0",
@daniel-hauser
Copy link
Owner

This error is related to emotion-js/emotion#2801 and will happen in SSR only.
The best way to mitigate it until fixed will render null in the server (i.e. { window ? <BinaryTree /> : null })

@kimjisena
Copy link
Contributor Author

Tried to render unconditionally on the server by checking if window is defined, it doesn't seem to work.

I had to install @emotion/css as a project dependency and copied over your code into my project to get it to work.

There has to be a better way.

@dance-cmdr
Copy link

This error also prevents devs from using react-organizational-chart with next.js.

The use case is to render a sitemap. It has to be done statically or server-side for SEO purposes.

Screenshot 2022-10-20 at 10 31 40

@yanalshoubaki
Copy link

I was have the same issue, how i fixed it :
First create component with only the chart component like this
image

then in the main component :
image

image

this will fix the error.

@nayakayp
Copy link

I was have the same issue, how i fixed it : First create component with only the chart component like this image

then in the main component : image

image

this will fix the error.

I run in the local nextjs environment, and there's no error but when deployed in production, the error is raised.
This fixed the error on build production :)

@brafiadi
Copy link

I was have the same issue, how i fixed it : First create component with only the chart component like this image

then in the main component : image

image

this will fix the error.

still, get the error when deployed to production

@brafiadi
Copy link

I try to import modules only in client side, and it's work on production

image

@rayaanr
Copy link

rayaanr commented Feb 6, 2024

Place the 'use client' directive at the top of your page file for NEXT (App Route)

'use client'

import React from 'react';
import { Tree, TreeNode } from 'react-organizational-chart';

const ExampleTree = () => (
  <Tree label={<div>Root</div>}>
    <TreeNode label={<div>Child 1</div>}>
      <TreeNode label={<div>Grand Child</div>} />
    </TreeNode>
  </Tree>
);

export default ExampleTree;

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

No branches or pull requests

7 participants