0

I was following this StackOverflow link

and 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>
Zack Zilic
  • 832
  • 3
  • 12
  • 28
  • If you look at the question you linked, you'll see to able to load HTML like this you need an appropriate loader. What it means is that require directive is not understood by the browser, and you need some tool that changes that require into the appropriate html import tag... Are you using webpack or some other bundler? – Fabio Lolli Jun 11 '18 at 09:36
  • Also, on a separate note, are you really sure you need to import HTML for a React component? I usually consider it a bad pattern, and it can be worked around in most use cases... Most, not all :) – Fabio Lolli Jun 11 '18 at 09:39
  • yes. I'm using webpack! Well, I was asked by my mentor to do this so I'm trying to make it happen. Looks possible. – Zack Zilic Jun 11 '18 at 09:40
  • 1
    Alright, if you're using webpack then @nrgwsth answer should have it covered – Fabio Lolli Jun 11 '18 at 09:42

1 Answers1

2

Use html-loader to import raw html in your code,

in your webpack config

{
      test: /\.(html)$/,
      use: {
        loader: 'html-loader'
      }
}
jbyun94
  • 25
  • 6
Anurag Awasthi
  • 6,115
  • 2
  • 18
  • 32