Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ReactJS needs compilation

Tags:

reactjs

jsx

Reactjs is consists of .js files and JavaScript doesn't compile as it's an interpreted language. Then why Reactjs need to be compiled? I read that it uses the JSX compiler.

like image 977
solveit Avatar asked Sep 04 '20 18:09

solveit


Video Answer


1 Answers

JSX includes markup syntax that isn't valid javascript. The JSX "compiler" translates the markup into vanilla javascript. You can see this yourself using the babel repl:

JSX in:

const Test = () => <div>test</div>

Javascript out:

"use strict";

var Test = function Test() {
  return /*#__PURE__*/React.createElement("div", null, "test");
};

There's also the matter of bundling, resolving imports, adding polyfills, etc.

like image 99
ray hatfield Avatar answered Sep 28 '22 03:09

ray hatfield