2

Current Implementation

Container

export const FormContainer = () => {
    return (<Form/>);
  }

Component

export const Form = () => {
    return ReactDOM.createPortal(
      <aside>
        <div>{"I am a component"}<div/>
      </aside>,
      document.body
    );
  }

Errors

ReferenceError: document is not defined

Expectation

I want the Form to be out of FormContainer DOM hierarchy.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Harsh Nagalla
  • 1,198
  • 5
  • 14
  • 26

1 Answers1

2

ReferenceError: document is not defined

ReactDOM.createPortal is more like a "BrowserSide only" component because it needs document and in serverSide Rendering, it's undefined.

I want the Form to be out of FormContainer DOM hierarchy.

easiest solution: set a condition to check for BrowserOnly rendering of createPortal component :

according to saimeunt answer, something like

export const FormContainer = () => {
    return (
        {process.browser?
            <Form/>:null
        }
    );
}

will make the day.

another way around will be using nextJs dynamic import and disable SSR on module

import dynamic from "next/dynamic";
const Form = dynamic(() => import("path/to/FormModule"), { ssr: false });


export const FormContainer = () => {
    return (<Form/>);
  }
Soren Abedi
  • 66
  • 1
  • 5