Element Variables in React
Learn how to store JSX elements in variables and render them inside React components using simple examples from CIIT Training Institute.
1. What Are Element Variables?
In React, an element variable is a variable that stores a
React element or JSX expression. We can create the element
once and use that variable inside the component's
return statement.
Element variables are useful when we want to prepare JSX separately and then display it in the user interface.
const message = <h2>Welcome!</h2>;.
We can then render that variable using
{' {message} '} inside JSX.
2. Syntax of Element Variables
The basic syntax is:
const element = JSX;
{'return {element};'}
function App() {
const message = <h2>Welcome to CIIT!</h2>;
return (
<div>
{message}
</div>
);
}
export default App;
Code Explanation
message stores the JSX heading element.
The component returns a div element.
{'{message}'} inserts the stored
React element into the returned JSX.
Expected Output
Welcome to CIIT!
3. How Element Variables Work
A React component creates an element variable, returns JSX, and uses the variable wherever that element should appear.
Create a Variable
Store JSX in a variable.
Return JSX
Write the component's UI.
Use the Variable
Insert it using curly braces.
Rendered UI
The element appears in the browser.
4. Example: Display a Student Message
CIIT Training Institute can store a student message in a variable and display it inside a React component.
function StudentMessage() {
const studentMessage = (
<div>
<h2>Hello, Student!</h2>
<p>Welcome to CIIT Training Institute.</p>
</div>
);
return (
<section>
{studentMessage}
</section>
);
}
export default StudentMessage;
Expected Output
Hello, Student!
Welcome to CIIT Training Institute.
studentMessage variable stores a JSX
div containing a heading and a paragraph.
Using {'{studentMessage}'} renders both elements.
5. Element Variables with Conditional Rendering
Element variables can also work with conditional rendering. We can assign different JSX elements to the same variable depending on a condition.
function StudentStatus() {
const isRegistered = true;
let message;
if (isRegistered) {
message = <h2>Registration Successful!</h2>;
} else {
message = <h2>Please Register First.</h2>;
}
return (
<div>
{message}
</div>
);
}
export default StudentStatus;
Code Explanation
-
isRegisteredrepresents the registration status. -
messageis declared to hold a JSX element. -
The
if-elsestatement assigns the appropriate JSX element tomessage. -
{'{message}'}renders the selected element.
Expected Output
Registration Successful!
isRegistered to false.
The output will become Please Register First.
6. Example: Element Variable with a List
We can also store a JSX element containing a list of courses in a variable.
function CourseList() {
const courseList = (
<ul>
<li>React Development</li>
<li>ASP.NET Core MVC</li>
<li>Data Analytics</li>
</ul>
);
return (
<div>
<h2>Our Courses</h2>
{courseList}
</div>
);
}
export default CourseList;
Expected Output
Our Courses
- React Development
- ASP.NET Core MVC
- Data Analytics
7. Element Variables vs Direct JSX
| Element Variable | Direct JSX |
|---|---|
| JSX is stored in a variable before rendering. | JSX is written directly inside the return statement. |
| Useful when an element is prepared separately or selected conditionally. | Useful for simple and straightforward UI. |
const title = <h2>Welcome</h2>;
|
{'return
|
8. Common Mistakes to Avoid
1. Forgetting curly braces
Incorrect:
return (
<div>
message
</div>
);
Correct:
return (
<div>
{message}
</div>
);
Without curly braces, message is treated
as text instead of a JavaScript variable expression.
2. Assigning a string instead of JSX
const message = "<h2>Welcome!</h2>";
This stores a string, not a React element. Rendering it displays the text rather than creating an HTML heading.
Correct:
const message = <h2>Welcome!</h2>;
9. Practice Tasks
Complete these tasks using React:
-
Create a variable named
welcomeMessagethat stores a heading welcoming students. -
Create a
courseInfovariable containing a heading and a paragraph about a React course. -
Create an
admissionMessageelement variable using anif-elsestatement. - Store an unordered list of three CIIT courses in a variable and render it.
- Create an element variable that displays different messages based on a student's result.
return statement.
Summary
Element variables allow React developers to store JSX elements in variables and render them inside a component. A variable can store a heading, paragraph, list, or a complete JSX structure. Curly braces are used to insert the variable inside JSX. Element variables can also be assigned conditionally using if-else statements to display different elements based on a condition. They help organize JSX and make conditional rendering easier to understand.