Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

Return a Variable in the Render Function in React

Gaurav Singhal

  • Nov 10, 2020
  • 5 Min read
  • Nov 10, 2020
  • 5 Min read
Web Development
Client-side Frameworks
Front End Web Development


JSX stands for JavaScript XML, a coding standard that allows you to use JavaScript expressions and other HTML features inline. Using JSX, you can create a function and return a set of JSX elements to a variable, and that variable used is to render the elements inside the render() function in React.

This guide will help you learn how to return JSX to a variable and render JSX markups to the DOM.

Return Static JSX Content to a Variable

JSX is used along with React to remove loosely coupled parts of a component, like HTML, CSS, and JavaScript, and get them to work together in a single file called a component. Its syntax can be easy to write and visually compelling. Thus, the JSX elements can also get returned to the variable, and that variable can be used for rendering.

For an example demonstration, let's create a static table within a function and return it to the variable. We'll also create one static <table> inside the function.

1getTable() {
2    return (
3      <div>
4        <table border="2">
5          <tr>
6            <td>Row 1 Col 1</td>
7            <td>Row 1 Col 2</td>
8          </tr>
9          <tr>
10            <td>Row 2 Col 1</td>
11            <td>Row 2 Col 2</td>
12          </tr>
13          <tr>
14            <td>Row 3 Col 1</td>
15            <td>Row 3 Col 2</td>
16          </tr>
17        </table>
18      </div>
19    );

The table is created within the function and returns the set of JSX markups to the function. So once you access the function, respective JSX markups of the table will be replaced.

The next step is to call the function and assign it to the variable, as shown below.

1render() {
2    // Calling function and assigned it to the variable
3    const mytable = this.getTable();
5    return (
6      <>
7        {/* rendered the JSX */}
8        <div>{mytable}</div> 
9      </>
10    );

The functionthis.getTable() is called and it returns the complete <table> element, so once you place it inside the return(), the element will be created into the DOM.

Conditionally Rendering JSX

Both static and dynamic content can be served to the DOM. Hence, you can render dynamic content based on individual conditional operators such as the ternary operator. For example, if you want to show active or inactive logged-in user status, the status color or label may change based on the condition, such as changing to green or red.

For clearer understanding, create two functions showing the active status label and the inactive status label.

1activeContent() {
2   return <label>Active</label>;
5inActiveContent() {
6   return <label>In-active</label>;

Each of the above functions returns the specific JSX element as a JSX <label> element, however, you should have some identifiers or conditions to dynamically show the status.

Create the key into the state object, as shown below.

1constructor(props) {
2    super(props);
3    this.state = {
4      isActive: false
5    };

Observe that there is a key named isActive in the component, and the same variable can be used as a condition that either can be true or false.

Now, let's call the above two functions and put their result in additional variables.

1const activeVariable = this.activeContent();
2const inactiveVariable = this.inActiveContent();

activeVariable is used to hold the active label status, and inactiveVariable holds the inactive status label. Both can be interchangeable using the ternary operator, as shown below.

1render() {
2    const activeVariable = this.activeContent();
3    const inactiveVariable = this.inActiveContent();
5    return (
6      <>
7        {this.state.isActive ? activeVariable : inactiveVariable}
8      </>
9    );

The above ternary condition uses this.state.isActive, and based on its value (true or false), the respective label will be rendered into the DOM. Using a variable in render() is a common approach. Most React developers use them to manage dynamic content from the function implementation, and they are easy to operate throughout the component.


JSX with React is not a required approach, but it can be effectively used to use JavaScript-based expressions and other features inline with HTML markup.

Returning a variable in the render function is the easiest way to manage the JSX markup, whether the content is static or dynamic. Give it a try and dig into JSX!

Learn More

Explore these React courses from Pluralsight to continue learning: