Why does react display my functional component at the bottom of my page?

Hugo

My main page looks like this :

import React from "react";
import { Fragment } from "react";
import ModalA from "../components/Modal/ModalOptionA";

export default class AePage extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <Fragment>    
        <div className="grid-intro">
          <div className="text-intro">
            Some Text
          </div>
          <div className="modal-component-insert">
            <ModalA show={true}/>
          </div>
          <div className="text-outro">
            Some text
          </div>
        </div>
      </Fragment>
    )
  }
}

And my component looks like this :

 import React from "react";
 import ReactDOM from "react-dom";

const Modal = ({ show, closed}) => {
  return (
    ReactDOM.createPortal(
      <>
      <div className="modal">
        My Component
      </div>
      </>,
    document.body
    )
  )  
}
export default Modal;

The code above display something like :

Some Text
Some Text
My Component

Why does my component not display between the texts ? Is there a specific way for React to display this component between my divs ?

Paweł Janik

That is what ReactDOM.createPortal is for. It will always move things to the bottom of the DOM. That way, you can then position it above everything else using CSS.

It seems you don't really need that, so I'd just replace your code for the Modal component with:

import React from "react";

const Modal = ({ show, closed}) => {
  return (
      <div className="modal">
        My Component
      </div>
  )  
}
export default Modal;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my React functional component fail to rerender when I change a (boolean) state property value?

React: Why won't this basic data populate in my functional component?

why doesn't toFixed() work in my React functional component?

Why my App.js use Functional Component as default React?

Why does my React Component Export not work?

Why my components don't display on my React page?

Why does my background stretch to the edges on the top of the page, but not on the bottom?

Why does my website load and show the bottom of the page first?

why does React.js doesn't display my component separately in a new window?

Why is my functional component's state undefined?

Why is my functional component not re-rendering?

React: Parsing error in my functional component

Why does my component not update without refresh page?

React Renders at the bottom of my component! no the top

Why is my axios post returning undefined in my functional component?

Why does my html select element not display on the page?

React JS page went blank after I used a setState() in my functional component

Why does my React Native bridged iOS component not work?

Why does my react component keep re-rendering?

Why does React dev tools show my component as Anonymous?

Why does my component rerender with React.memo

Why my React component does not rerender when Set state was changed?

Why does my react state update on component re render?

Why is my footer not showing on the bottom of the page?

Why is my React functional component returning several times? I need it to run only once

Why is my functional react component's state throwing TypeError: state is undefined?

Why does my content not display?

why does my selectlist not display?

Why does my pressable component display my off image on first tap and then works as expected?