Author avatar

Gaurav Singhal

Return a Variable in the Render Function in React

Gaurav Singhal

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

Introduction

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.

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

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.

1
2
3
4
5
6
7
8
9
10
11
render() {
    // Calling function and assigned it to the variable
    const mytable = this.getTable();

    return (
      <>
        {/* rendered the JSX */}
        <div>{mytable}</div> 
      </>
    );
}
jsx

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.

1
2
3
4
5
6
7
activeContent() {
   return <label>Active</label>;
}

inActiveContent() {
   return <label>In-active</label>;
}
jsx

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.

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

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.

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

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.

1
2
3
4
5
6
7
8
9
10
render() {
    const activeVariable = this.activeContent();
    const inactiveVariable = this.inActiveContent();

    return (
      <>
        {this.state.isActive ? activeVariable : inactiveVariable}
      </>
    );
}
jsx

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 developera use them to manage dynamic content from the function implementation, and they are easy to operate throughout the component.

Conclusion

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!

1