21

I want to import an HTML file into a React component

enter image description here

peft.html

<!DOCTYPE html>
<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
  </body>
</html>

index.js

import perf from perf.html

class Index extends React.Component {
   render(){
      return (
         <iframe src="perf"></iframe>   /* like this */
         <iframe src="./perf"></iframe> /* or like this */
         /* or like any possible way */
      );
   }
}
export default Index;

so in my case, I need to import a local HTML file and use it in but I'm getting the error:

Uncaught (in promise) Error: Module parse failed: C:\Users....\target.html Unexpected token (1:0) You may need an appropriate loader to handle this file type.

any ideas? How to load an HTML file inside of a React Component? How to add an appropriate loader to handle this file type? Anything?

I find this solution as most logical:

var __html = require('./template.html');
var template = { __html: __html };

React.module.exports = React.createClass({
  render: function() {
    return(
      <div dangerouslySetInnerHTML={template} />
    );
  }
});

but I can not apply it because I'm getting an error:

Uncaught (in promise) Error: Module parse failed: C:\Users\....\target.html Unexpected token (1:0) You may need an appropriate loader to handle this file type. 
| <!DOCTYPE html> 
| <html xmlns="w3.org/1999/xhtml">; 
| <head><title>
2
  • 1
    Look at: stackoverflow.com/questions/33973757/… Commented Jun 11, 2018 at 8:05
  • Why not make it peft.js instead of .html extension? Is there any reason?. You can also use this "const component(){ return(<div>hello World</div>)}" Commented Jun 11, 2018 at 8:13

2 Answers 2

17

You can import or require the html file and use it in the src attribute,

var perf =require('./template.html');

class Index extends React.Component {
   render(){
      return (
         <iframe src={perf }></iframe>   /* like this */
      );
   }
}
export default Index;
Sign up to request clarification or add additional context in comments.

5 Comments

But where are you using IndexedDB ??
I am curious to know the answer for this problem. I am running into the same error as mentioned in the post. The answer given above "<iframe src={perf }></iframe> " didn't solve my problem.
Did you try <div dangerouslySetInnerHTML={{ __html: htmlText }} ></div>
u still get Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See webpack.js.org/concepts#loaders > <!DOCTYPE html> | <html lang="en"> | <head>
Works fine with Create React App, see github.com/facebook/create-react-app
13

Spent 2 hours on this.

Two ways to do the same below:

  1. Method1 - iframe (part of page) goes to that html file (chap3.htm)
  2. Method2 - fetch the contents of chap3.htm and set to state, state is added with dangerouslySetInnerHTML

NOTE

  1. Html file is in public directory (myapp\public\chap3.htm).
  2. May work from current directory too, but does not look like as a usual way to load static resource/html from current directory.

Method 1

import React from'react';
import'./App.css';

function App() {
  return(
    <div className="App">
      <iframe src="chap3.htm"></iframe>
    </div>
  );
}

export default App;

Method 2

import React, { useEffect, useState }from'react';
import'./App.css';

function App() {
  let[htmlFileString, setHtmlFileString] = useState();

  async function fetchHtml() {
    setHtmlFileString(await (await fetch(`chap3.htm`)).text());
  }
  useEffect(() => {
    fetchHtml();
  }, []);

  return(
    <div className="App">
      <div dangerouslySetInnerHTML={{ __html: htmlFileString }}></div>
    </div>
  );
}

export default App;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.