3

My React application is generated by create-react-app. I am trying to display a html file content(exists under directory src/www/test.html) in react component.

Running "npm start" is throwing error:

Compiling... Failed to compile.

./src/www/test.html 1:0 Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type.

| |

React Component

import Page from '../../www/test.html';

class Example extends React.Component {

render(){
    let htmlPage = {__html : Page};
    return (<div dangerouslySetInnerHTML={htmlPage} /> );
}

}

================

test.html

<html>
<head></head>
<body>
    <h1>I'm alive!</h1>
</body>
</html>

Please note, I don't prefer to eject from create-react-app. Looking for suggestions to overcome this issue.

4
  • I can't be sure, but I'm pretty sure you can't import HTML files into JS. changes the HTML file to a jsx file with a corresponding export call? Commented Jan 15, 2019 at 1:15
  • You can when using webpack (which is used internally by create-react-app). I'm pretty sure that this should work since create-react-app doesn't specify its own loader for .html files and webpack should handle .html files by default (returning a string of the content) Commented Jan 15, 2019 at 1:29
  • Thanks for your suggestion. Without ejecting from create-react-app how to modify the webpack config to add html loader. Commented Jan 15, 2019 at 1:43
  • Perhaps This question can give you some guidance. Commented Jan 15, 2019 at 4:06

0

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.