Author avatar

Gaurav Singhal

React.js br Tag and AJAX Request

Gaurav Singhal

  • Sep 15, 2020
  • 6 Min read
  • Sep 15, 2020
  • 6 Min read
Web Development
Front End Web Development
Client-side Framework


While working with a web app, you'll need to access data from the server. For that, you need to implement the AJAX call using either fetch() or another third-party library such as Axios. In React, you use the self-closing <br> tag to produce a line break between the text or a section, whereas in HTML you'd use <br> … </br>. Through this guide, you will learn to use the <br> tag and AJAX calls in detail.

Implementing AJAX Calls

Data accessibility is a primary requirement for any web or mobile app to fetch the data from the server. Hence, you need to implement the AJAX call by providing the API URL from where you are going to bring the data.

AJAX Call Using fetch()

fetch() is an inbuilt API in the browser that allows you to make network calls based on XMLHttpRequest protocol, used to consume the resources across the network.

One of the most accessible options to make the network call is fetch(), where you need to provide the server URL along with the other parameters such as request header, request type, and so on.

Below is the simple syntax using fetch().

2  .then(response => response.json())
3  .then(data => console.log(data));

Along with the fetch API, you need to pass the server API URL from where you fetch the resources. The response will return the result if you succeed; otherwise, you need to handle the error handling.

Below is the complete example to fetch the data from the server along with the error handling section.

1componentDidMount() {
2    fetch("")
3      .then(res => res.json())
4      .then(
5        result => {
6          this.setState({
7            users: result
8          });
9        },
10        error => {
11          // error handling
12          console.log(error);
13        }
14      );

The above example uses one dummy API URL to fetch the data, and once the API is successful, the response used in your React component. Otherwise, the error section executes if the API fails to return the result.

AJAX Call Using a Third-party Library

You can apply to fetch() API, or else there are tons of third-party libraries available to make the network calls such as Axios.

You need to install the third-party libraries in the React app before using it. For example, you can install the Axios library using the below command.

1npm install axios

After installing the library, you can use the Axios package to make the network calls; below is the simple syntax that shows how to use Axios in the component.

2    .get("API_URL")
3    .then(function(response) {
4    // use response

The syntax of Axios is the same as fetch(), where you need to pass the API URL followed by the then section where you can manage the response of the API.

Below is the complete example that shows how to use the Axios library.

1componentDidMount() {
2    var self = this;
3    axios
4      .get("")
5      .then(function(response) {
6        self.setState({ users: });
7      });

As you can see in the above example, you can use the get() API call, followed by the API URL. Once the response comes from the server, the response data gets stored in the local state of the component.

You can manage the error handling is as shown below.

1componentDidMount() {
2    var self = this;
3    axios
4      .get("")
5      .then(function(response) {
6        self.setState({ users: });
7      })
8      .catch(function(error) {
9        console.log(error);
10      })
11      .finally(function() {
12        // Always executed with the try block
13      });

There are two additional sections added called catch() and finally() which manage the request/response error.

Using <br> Tag in React

We use the <br> tag in HTML to break the string in between or break the section; hence, if you want to break the string, the <br> tag will be placed, and the followed content will move to the next line.

For example, there is one string, and you can use the <br>, as given below.

2 <body>
3  <span>This is first one <br> This is second one</span>
4 </body>

In between the <span> tag, there is one <br> tag placed. So, once you run the above example, the output should look like this:

1This is first one 
2This is second one

But in React, the <br> is used differently, which means the br tag will be used as a self-closing tag, whereas in HTML, it can be used just like <br> to break the section.

1render() {
2    return (
3      <div>
4        <p>BR tag in React</p>
5        <hr />
6        <span>SPAN1</span><br/>
7        <span>SPAN2</span>
8      </div>
9    );

The above example uses two different tags: <p> and <span>. In between them, there is a self-closing <br/> tag used.

As you run the above example, all the sections will move to a new line rather than the same line of the DOM.

But if you use <br> without the slash, then you will get the following error.

1Expected corresponding JSX closing tag for 'br'.


AJAX calls are a vital part of any app to fetch the resources from the server, and the <br/> tag is one of the solutions to break the section immediately after being used in between the DOM elements.

I hope this guide will get your attention to learn about the AJAX calls and using <br> tag in React efficiently. Keep learning!