Skip to content

Instantly share code, notes, and snippets.

@philschmid
Last active June 14, 2023 18:47
Show Gist options
  • Save philschmid/2f0e574aa72f7f9535e18f06d70db8d0 to your computer and use it in GitHub Desktop.
Save philschmid/2f0e574aa72f7f9535e18f06d70db8d0 to your computer and use it in GitHub Desktop.
JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. Although there are other ways to write components, most React developers prefer the conciseness of JSX, and most codebases use it.
You will learn
Why React mixes markup with rendering logic
How JSX is different from HTML
How to display information with JSX
JSX: Putting markup into JavaScript
The Web has been built on HTML, CSS, and JavaScript. For many years, web developers kept content in HTML, design in CSS, and logic in JavaScript—often in separate files! Content was marked up inside HTML while the page’s logic lived separately in JavaScript:
HTML markup with purple background and a div with two child tags: p and form.
Three JavaScript handlers with yellow background: onSubmit, onLogin, and onClick.
But as the Web became more interactive, logic increasingly determined content. JavaScript was in charge of the HTML! This is why in React, rendering logic and markup live together in the same place—components.
React component with HTML and JavaScript from previous examples mixed. Function name is Sidebar which calls the function isLoggedIn, highlighted in yellow. Nested inside the function highlighted in purple is the p tag from before, and a Form tag referencing the component shown in the next diagram.
Sidebar.js React component
React component with HTML and JavaScript from previous examples mixed. Function name is Form containing two handlers onClick and onSubmit highlighted in yellow. Following the handlers is HTML highlighted in purple. The HTML contains a form element with a nested input element, each with an onClick prop.
Form.js React component
Keeping a button’s rendering logic and markup together ensures that they stay in sync with each other on every edit. Conversely, details that are unrelated, such as the button’s markup and a sidebar’s markup, are isolated from each other, making it safer to change either of them on their own.
Each React component is a JavaScript function that may contain some markup that React renders into the browser. React components use a syntax extension called JSX to represent that markup. JSX looks a lot like HTML, but it is a bit stricter and can display dynamic information. The best way to understand this is to convert some HTML markup to JSX markup.
CONTEXT:
QUESTION:
Do I need to use JSX with React?
You are an heplful and honest assistant specializing in React.
Your goal is to provide detailed answers to QUESTION related to React. First, analyze the question and the provided CONTEXT break it down into smaller components if necessary.
Then, generate a comprehensive response that covers all the relevant aspects. Use the provided context to ensure the response is accurate and informative. Provide the source in the response from all the information you used to answer the question at the end of your answer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment