
What is JSX?
We utilize JSX to create HTML-like code in JavaScript when we’re using React. It looks and feels like HTML that is married to JavaScript (because it is actually JavaScript).
JSX is helpful since it makes the component more readable.
If you have not read my previous posts about React yet, here are a few points for you:
- You can only return one element at a time when you’re creating a component using JSX. This is why we use a parent element. For instance, <div>
Welcome.jsx
const Welcome= () => {
return (
<div>
<h1>Daily Bugle</h1>
<p>Welcome to the best news outlet there where we keep this creep Spiderman in check!</p>
</div>
);
};
- If the JSX spans multiple lines, wrap it in parentheses for clarity just like the one above
- If you’re only returning a single line, you can just add the JSX after the return statement to return to it
SpidermanPoetry.jsx
const SpidermanPoetry= () => {
return <p>I have no money. No parents left alive. Spidey has no home.</p>;
};
- Since JavaScript is under the hood of React, we can also perform calculation in JSX
BudgetCalculations.jsx
const BudgetCalculations = () => {
return <h1>{5+5}</h1>;
};
- Embedded expression must go in between the curly braces just like what we have above
- Since JSX is JavaScript, it gives us the full power of JavaScript! You can also reference variables inside JSX
Welcome.jsx
const name= "Tony Stark";
const Welcome = () => {
return <h1>Welcome Home, {name}</h1>;
};
- That means we can perform calculations, reference variable, or use any valid Javascript expression inside the curly braces
Welcome.jsx
const score= 15;
const Welcome = () => {
return <h1>{score * 10}</h1>;
};
- Therefore, we use embedded expression to insert JavaScript and JSX in other JSX elements
Profile.jsx
const name = "Peter Parker";
const Profile = () => {
return <h1>{name}</h1>;
};
- In JSX, you can use all the tags you already know from regular HTML, including <div>, <h1>, <p>, <footer>, etc.
- A reminder that some elements in HTML are self-closing which means that they only have a single tage that ends with a slash /
MyPage.jsx
const MyPage = () => {
return (
<div>
<h1>Ducks are great</h1>
<p>I love ducks and bubble baths</p>
<img src="https://shorturl.at/lUn8R" alt="Batmetal Returns by Jack Blin" />
<br />
<input type="text" placeholder="Type here..." />
</div>
);
};
- You can also store JSX inside a variable. This is really useful when you want to write JSX in some part of your code and then you want to refer to it somewhere else
- You can also reference other JSX elements using a JSX expression with curly braces
- And if you want to include multiple JSX expressions within one component, you need to use the {} for each expression
- You can use embedded expressions to pass values into a JSX elements attributes
MyPage.jsx
const nameHeader = <h1>BatMetal Forever!</h1>;
const age = 37;
const url = "https://shorturl.at/lUn8R";
const MyPage = () => {
return (
<div>
{nameHeader}
<p>I love ducks and bubble baths!</p>
<p>I am {age} years old.</p>
<br />
<img src={url} alt="Batmetal Returns by Jack Blin" />
</div>
);
};
In Summary
JSX is a unique syntax that blends JavaScript with HTML.
It as a bridge between the logic of JavaScript and the structure of HTML. With JSX, you can write HTML-like code directly in your JavaScript files, allowing you to describe the layout of your UI in a way that’s both visual and declarative.
This makes your code more readable and it also simplifies the process of building dynamic web applications by embedding expressions and JavaScript logic right into the markup.
Reference: react.dev and my school notes 💋😝
-
Implementing Dark Mode in React
We have previously talked about useState before. A great way to create CSS classes on the fly is to use state variable! Here is an example of how to create Dark Mode in React by using string interpolation to construct the individual classes based on the state variable mode. index.html index.jsx App.jsx App.css So, if…
-
Understanding Props in React: A Comprehensive Guide
This content explains the concept of props in React, which allow data to be passed to components, making them dynamic. It covers how to access props as objects, pass multiple properties, and handle different data types. The importance of correctly rendering non-string values and using props in JSX is emphasized.
-
The Power of JSX: Combining HTML and JavaScript
JSX is a syntax extension for JavaScript that resembles HTML, used primarily in React to create components. It allows developers to write HTML-like code within JavaScript, enhancing readability and enabling dynamic web applications. With JSX, one can embed JavaScript expressions, making the integration of logic and UI layout seamless.
-
Organizing React Components for Better Code
The content discusses best practices for organizing React components in separate files for maintainability. It details the process of importing React, creating functional components, and exporting them. Additionally, it covers returning multiple elements and using named vs. default exports, emphasizing strategic organization for better code management.
-
Step-by-Step Guide to Rendering React Components
The content provides a step-by-step guide on rendering components in React. It emphasizes creating and linking HTML and JavaScript files, setting up the main component, and utilizing the createRoot function to manage rendering. The author injects humor while guiding readers through the essential concepts of React component rendering.
-
Introduction to React: Understanding the Basics
React is a JavaScript library for creating single-page web applications that dynamically update without page reloads. It organizes code into reusable components, akin to functions returning HTML-like JSX. This enhances development efficiency, as components can be reused, resembling custom HTML tags. Overall, React simplifies coding while improving functionality.
-
HTML Structure Explained: A Beginner’s Guide
The content discusses HTML, the foundational markup language for web development. It highlights its structure, including elements like tags and attributes. The post humorously reflects on the author’s learning journey while encouraging newbies by simplifying concepts like nesting elements and the importance of HTML in web design. It concludes with code examples.
-
Why Use Pure Functions in React Components?
Pure functions in JavaScript are self-contained and produce consistent results without affecting external variables or objects. They enhance clarity and maintainability in code, minimizing bugs. Unlike impure components that modify existing variables, pure components receive data as props. Understanding component relationships, like the React render tree and module dependency tree, further optimizes performance and structure.
-
React Components Explained: A Guide for Developers
This post analyzes React components, highlighting their crucial role in creating dynamic user interfaces. It details the types of components, including functional and class components, and explains props and state. Additionally, it covers lifecycle methods and emphasizes the importance of reusability and composition in building complex applications effectively.