-3

I'm making a system in react but for some reason index.html is not running index.js by the following error. I've tried a lot and it doesn't work.

IMAGES: error in index.js index.html execute

OBS: I've tried to change the type to 'text/jsx' but it doesn't work either.

  • Does this answer your question? [ReactJS: "Uncaught SyntaxError: Unexpected token <"](https://stackoverflow.com/questions/28100644/reactjs-uncaught-syntaxerror-unexpected-token) – Digital Deception Jan 31 '22 at 02:25

3 Answers3

-1

You are missing an important step, transpiling your code into something readable. JSX is just syntactic sugar on top of React for .createElement and is not usable without a compilation step.

The short answer is that unless you have a specific reason for trying to do this (e.g., you're trying to learn/understand), I would suggest you use an out of the box tool such as NextJS or create-react-app as they will give you a production ready solution. Alternatively, look at using React without JSX. I'm guessing you were trying to do something like what is mentioned in this question: ReactJS: "Uncaught SyntaxError: Unexpected token <". However, you haven't provided enough context for us to help with that or your motivation for doing so.

Diving a bit more into the details, your compilation tool (e.g., babel) will take JSX and compile it into code that plain JS can understand.

const HelloWorld = ({name}) => <div>Hello {name}</div>
const MyCoolElement = () => <HelloWorld name='Bob' />

ReactDOM.render(<MyCoolElement>, document.getElementById('root));

becomes something like:

const HelloWorld = ({name}) => React.createElement('div', null, `Hello ${name}`);

const MyCoolElement = () => React.createElement(HelloWorld, {name:'Bob'}, null);

ReactDOM.render(React.createElement(MyCoolElement, {}, null), document.getElementById('root));
Digital Deception
  • 2,677
  • 2
  • 15
  • 24
-2

Are you using webpack? I dont see where the javascript libraries are included...

N S Niko
  • 77
  • 2
-2

Browsers can't read jsx. You can try this approach: https://dev.to/devalnor/running-jsx-in-your-browser-without-babel-1agc