JSX is a syntax extension for JavaScript that allows you to write HTML-like code within your JavaScript files. It stands for JavaScript XML. JSX makes it easier to write and understand React components by blending HTML and JavaScript syntax together.
JSX simplifies the process of building user interfaces in React by providing a more expressive and readable syntax. Instead of writing separate JavaScript and HTML code, JSX allows you to write both in a single file, making it easier to visualize and understand the component’s structure.
In JSX, you can embed JavaScript expressions using curly braces {}
. This allows you to dynamically insert values or execute JavaScript code within your JSX code.
const name = "John";
const element = Hello, {name}
;
{name}
is a JavaScript expression embedded within JSX.{name}
is replaced with the value of the name
variable, resulting in the text “Hello, John” being rendered inside the <h1>
element.JSX elements look similar to HTML tags, but they are actually objects that represent React elements. You can use JSX elements to build the UI structure of your React components.
const element = Hello, React!;
<div>Hello, React!</div>
is a JSX element representing a <div>
element with the text “Hello, React!” inside it.To render JSX elements in your React application, you can use ReactDOM’s render()
method.
import React from 'react';
import ReactDOM from 'react-dom';
const element = Hello, React!
;
ReactDOM.render(element, document.getElementById('root'));
<h1>Hello, React!</h1>
and store it in the element
variable.ReactDOM.render()
to render the element
onto the DOM. The render()
method takes two arguments: the JSX element to render and the DOM element where it should be rendered (document.getElementById('root')
).In React, JSX elements can have attributes just like HTML elements. These attributes are used to pass additional information to the elements or customize their behavior. Props (short for properties) are a way to pass data from parent to child components in React.
In JSX, you can add attributes to elements using the same syntax as HTML. However, JSX attributes are camelCase instead of lowercase.
const element = ;
<input>
element with two attributes: type
and placeholder
.type
attribute specifies the type of the input field, while the placeholder
attribute provides a hint to the user about what to enter in the input field.Props allow you to pass data from parent to child components in React. You can pass props to JSX elements by specifying them as attributes.
function Welcome(props) {
return Hello, {props.name}
;
}
const element = ;
Welcome
that takes a props
object as its argument.name
prop using props.name
and render it within an <h1>
element.Welcome
component, we pass the name
prop as an attribute with the value "John"
.name
prop is dynamically inserted into the JSX element when it is rendered.
// App.js or Index.js
import React from 'react';
import ReactDOM from 'react-dom';
function Welcome(props) {
return Hello, {props.name}
;
}
const element = ;
ReactDOM.render(element, document.getElementById('root'));
Welcome
that takes a props
object as its argument. Inside the component, we access the name
prop using props.name
and render it within an <h1>
element.<Welcome name="John" />
, where we pass the name
prop with the value "John"
.ReactDOM.render()
to render the element
onto the DOM. The rendered output will replace the content of the HTML element with the ID root
.
// index.html
Hello, John
Welcome
component is rendered with the name
prop set to “John“.<h1>
element on the webpage.Conditional rendering allows you to display different JSX elements based on certain conditions. This is commonly used to render different content based on the state of your application.
const isLoggedIn = true;
const element = (
{isLoggedIn ? Welcome, user!
: Please log in.
}
);
isLoggedIn
that determines whether the user is logged in or not.isLoggedIn ? ... : ...
) to conditionally render different JSX elements based on the value of isLoggedIn
.isLoggedIn
is true
, we render a <p>
element with the text “Welcome, user!”. Otherwise, we render a <p>
element with the text “Please log in.”.{}
is evaluated dynamically, allowing us to render different content based on the condition.You can use JavaScript’s map()
function to dynamically render lists of JSX elements based on the items in an array. This is useful for rendering dynamic content, such as a list of items fetched from an API.
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) => {number} );
const element = {listItems}
;
numbers
containing a list of numbers.map()
function to iterate over each item in the numbers
array and return a JSX <li>
element for each number. We also provide a unique key
prop for each <li>
element to help React identify them efficiently.listItems
) is then rendered inside a <ul>
element, creating a numbered list of items.You can apply inline styles to JSX elements using JavaScript objects. This allows you to dynamically style your components based on props, state, or other factors.
const style = {
color: 'blue',
fontSize: '20px',
};
const element = Styled Text
;
style
containing CSS properties and values.style
object as an attribute in the JSX element <p style={style}>
.style
object are camelCased, just like in regular CSS, but they are specified as strings in the JavaScript object.style
object to the <p>
element when it is rendered.
// project structure
my-react-app/
├── node_modules/
├── public/
│ ├── index.html
│ └── favicon.ico
├── src/
│ ├── components/
│ │ └── App.js
│ ├── App.css
│ ├── index.js
│ └── logo.svg
├── .gitignore
├── package-lock.json
├── package.json
└── README.md
import React from 'react';
import './App.css';
function App() {
const isLoggedIn = true;
const numbers = [1, 2, 3, 4, 5];
return (
Welcome to My React App!
{/* Conditional Rendering */}
{isLoggedIn ? Welcome, user!
: Please log in.
}
{/* Mapping Arrays */}
{numbers.map((number) => (
- {number}
))}
{/* Styling with JSX */}
Styled Text
);
}
export default App;
.App {
text-align: center;
margin-top: 50px;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
margin-bottom: 10px;
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.render(
,
document.getElementById('root')
);
My React App
When you run the application (npm start
), you’ll see the following output in your browser
The App
component in src/components/App.js
demonstrates various React functionalities:
isLoggedIn
variable.map()
function.The styles for the App
component are defined in src/App.css
.
The index.js
file in src
is the entry point of the application. It imports the App
component and renders it to the DOM.
The public/index.html
file is the main HTML file where the React application is mounted.
JSX is a powerful feature of React that allows you to write HTML-like code within your JavaScript files. It provides a more expressive and readable syntax for building user interfaces in React applications.Happy coding ! ❤️