Inject HTML into React using dangerouslySetInnerHTML

I am using React and fetching custom HTML headers and footers to be rendered above and below the main part of the React App (portal). These headers and footers are custom HTML and could be incomplete HTML on their own (but combined they would create complete HTML). For example the header may end in an opening tag like <main> and the footer could begin with the closing tag </main> with the portal nested inside.

I have tried to use React’s dangerouslySetInnerHTML, but it seems like it is closing all the tags and not putting the portal in the proper location. For example instead of the header ending in <main> it ends in <main></main>. The header, portal, and footer are all on the same level hierarchy in the DOM, instead of the portal being nested between the header’s opening tag and the footer’s closing tags.

basic example of header and footer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let header = '<h1>Welcome to Our Site</h1>
<main>'
let footer = '</main>
<p>Footer</p>'
</code>
<code>let header = '<h1>Welcome to Our Site</h1> <main>' let footer = '</main> <p>Footer</p>' </code>
let header = '<h1>Welcome to Our Site</h1>
              <main>'

let footer = '</main>
              <p>Footer</p>'

This doesn’t work in app.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div dangerouslySetInnerHTML={{__html: portal.headerHtml}}/>
<div id="portalMain">
<AppRoutes/>
</div>
<div dangerouslySetInnerHTML={{__html: portal.footerHtml}}/>
</code>
<code><div dangerouslySetInnerHTML={{__html: portal.headerHtml}}/> <div id="portalMain"> <AppRoutes/> </div> <div dangerouslySetInnerHTML={{__html: portal.footerHtml}}/> </code>
<div dangerouslySetInnerHTML={{__html: portal.headerHtml}}/>
     <div id="portalMain">
         <AppRoutes/>
     </div>
<div dangerouslySetInnerHTML={{__html: portal.footerHtml}}/>

This doesn’t put the portal in between the main tag, it renders the header, portal, and footer all on the same level (portal isn’t nested).

I tried putting the dangerouslySetInnerHTML in its own component, Refs, createPortal, and the parse function from html-react-parser.

What does work is in index.js setting the innerHTML of the root element and having React just handle the App and not using dangerouslySetInnerHTML in App.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> const rootElement = document.getElementById('root');
rootElement.innerHTML = `
${headerHtml}
<div id="react-root"></div>
${footerHtml}
`;
const reactRootElement = document.getElementById('react-root');
const root = createRoot(reactRootElement);
root.render(<App/>)
</code>
<code> const rootElement = document.getElementById('root'); rootElement.innerHTML = ` ${headerHtml} <div id="react-root"></div> ${footerHtml} `; const reactRootElement = document.getElementById('react-root'); const root = createRoot(reactRootElement); root.render(<App/>) </code>
  const rootElement = document.getElementById('root');

  rootElement.innerHTML = `
    ${headerHtml}
    <div id="react-root"></div>
    ${footerHtml}
  `;

  const reactRootElement = document.getElementById('react-root');
  const root = createRoot(reactRootElement);

  root.render(<App/>)

There has to be a better way to do this though right? I’ve been chatting with chatGPT and Claude for hours trying to figure out the best way. It seems like the broken HTML header and footer is making it challenging. It would be great if I could get the portal to be in the correct location and have the header and footer within the React App instead of outside of it.

New contributor

Logan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật