JSX Expressions in React JS 🎯

beginner
9 min

JSX Expressions in React JS 🎯

Welcome to the JSX Expressions lesson, where we dive into the magical world of JavaScript and HTML integration in React JS! Let's get started on this exciting journey together. 🚀

Understanding JSX 📝

JSX (JavaScript XML) is a syntax extension for JavaScript, developed by Facebook for the React library. It allows us to write HTML-like code within our JavaScript files, making it easier to describe the structure and appearance of our user interfaces.

JSX Expressions 💡

JSX Expressions are used to return HTML from a function, which can then be rendered to the DOM (Document Object Model) by React. Let's explore an example:

jsx
function Greeting(props) { return <h1>Hello, {props.name}!</h1>; }

In the above example, we have a function called Greeting that accepts a props object, which could contain data we want to display. Inside the function, we are using JSX to return an HTML-like structure (in this case, an h1 tag with the user's name).

Rendering JSX Expressions 💡

To render the JSX Expression, we need to use the ReactDOM.render() function. Here's an example:

jsx
const element = <h1>Hello, World!</h1>; const root = document.getElementById('root'); ReactDOM.render(element, root);

In the example above, we have created a JSX Expression and assigned it to the element variable. We then select the root DOM element (in this case, an HTML div with id 'root') and use the ReactDOM.render() function to render our JSX Expression to the DOM.

Quiz 💡

JSX Syntax 📝

JSX syntax closely resembles HTML syntax, with a few important differences:

  • JSX tags begin with an opening curly brace { and end with a closing curly brace }.
  • JSX values must be wrapped in parentheses ().
  • JSX attributes are written in camelCase.

JSX Variables and Expressions 💡

You can embed JavaScript expressions in JSX by wrapping them in curly braces {}. Here's an example:

jsx
function Message(props) { const message = 'Hello, ' + props.name + '!'; return <p>{message}</p>; }

In the example above, we have created a message variable using JavaScript expressions and then returned it as a JSX Expression within the p tag.

Quiz 💡

JSX Attributes 📝

JSX attributes are written in camelCase, and they correspond directly to HTML attributes. Here's an example:

jsx
function Button(props) { return <button className={props.className} onClick={props.onClick}>{props.children}</button>; }

In the example above, we have created a Button component with three attributes: className, onClick, and children. The className attribute corresponds to the class attribute in HTML, while the onClick attribute corresponds to the onclick attribute in HTML. The children prop represents the content of the button.

Quiz 💡

Conclusion 🎯

Congratulations! You've learned the basics of JSX Expressions in React JS. Now, you can create dynamic and expressive UI components using the power of JSX. Keep practicing and exploring, and you'll be a React JS pro in no time! 🏆

As always, if you have any questions or need further clarification, feel free to reach out. Happy coding! 🚀