Skip to content

How to load html file as string into React #9046

@ghost

Description

I want to load content of a HTML file as string and use dangerouslySetInnerHTML to put it in a div of React component.

For example:

<!-- staticPages/pattern.html -->
<div>
    <div>
        ...
    <div>
</div>
// pattern.jsx
import React from 'react';
import html from 'statisPages/pattern.html'

export default function Pattern() {
  return (
    <div dangerouslySetInnerHtml={{ __html: html }}>
    </div>
  );
} 

I know I can export all HTML as string from a js file, but to do so I have to put HTML inside string literal and it's doesn't look pretty, I want to put it in a normal *.html file so others know it's a HTML.

Webpack has html-loader that can do this, I tested and it worked but it required to eject.

Does anybody have idea to do this, or to use html-loader without ejecting ?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions