JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within your JavaScript code. It's primarily used with React, a popular JavaScript library developed by Facebook for building user interfaces. JSX makes it easier to write and manage the structure of components in a React application by blending the presentation layer (HTML) with the application logic (JavaScript).
JSX is not natively understood by web browsers and needs to be transpiled into standard JavaScript using tools like Babel before it can be executed in a browser.
Here's an example of JSX code:
const App = () => {
return (
<div>
<h1>Hello, world!</h1>
<p>Welcome to my React app.</p>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
In the example above, the App
component is defined using an arrow function that returns JSX. The JSX code looks like HTML but is actually JavaScript. The ReactDOM.render()
function is used to render the App
component into an HTML element with the ID root
.