ReactJS: “Uncaught SyntaxError: Unexpected token <”

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

I am trying to get started building a site in ReactJS. However, when I tried to put my JS in a separate file, I started getting this error: "Uncaught SyntaxError: Unexpected token

I tried adding /** @jsx React.DOM */ to the top of the JS file, but it didn't fix anything. Below are the HTML and JS files. Any ideas as to what is going wrong?

HTML

        Page

JS

/**  * @jsx React.DOM  */ var Lander = React.createClass({     render: function () {         var info = "Lorem ipsum dolor sit amet... ";         return(             
{info}
); } });

EDIT: I realized that I need to add type="text/jsx" to the script tag which includes my lander code. However, after adding this and reloading I get this warning

"You are using the in-browser JSX transformer. Be sure to precompile your JSX for production - http://facebook.github.io/react/docs/tooling-integration.html#jsx"

followed by this error:

"XMLHttpRequest cannot load file:///Users/.../lander.js. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource."

it seems like there is something else that I need to do in order to get in browser jsx transform working, but I'm not sure what it is.

EDIT: OOOOH do I need to host it using MAMP or something?

回答1:

Add type="text/jsx" to the src attribue of script tag used to include JavaScript file that must be transformed by JSX Transformer, like that:

Then you can use MAMP or some other service to host the page on localhost so that all of the inclusions work, as discussed here.

Thanks for all the help everyone!



回答2:

JSTransform is deprecated , please use babel instead.



回答3:

Add type="text/babel" as an attribute of the script tag, like this:



回答4:

Add type="text/babel" to the script that includes the .jsx file and add this:



回答5:

If you have something like

Uncaught SyntaxError: embedded: Unexpected token 

You probably missed a comma in a place like this:

  var CommentForm = React.createClass({   getInitialState: function() {       return {author: '', text: ''};    }, //                   )   }   }); 


回答6:

The code you have is correct. JSX code needs to be compiled to JS:

http://facebook.github.io/react/jsx-compiler.html



回答7:

If you are getting an error like this :

SyntaxError: embedded: Unexpected token (107:9) 105

It could be you are missing a curly bracket



回答8:

Try adding in webpack, it solved the similar issue in my project. Specially the "presets" part.

module: {     loaders: [         {             test: /\.jsx?/,             include: APP_DIR,             loader: 'babel',             query  :{                 presets:['react','es2015']             }         }, 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!