Author avatar

Gaurav Singhal

React.js br Tag and AJAX Request

Gaurav Singhal

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

Introduction

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().

1
2
3
fetch('API_URL')
  .then(response => response.json())
  .then(data => console.log(data));
jsx

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.

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

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.

1
npm install axios
shell

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.

1
2
3
4
5
axios
    .get("API_URL")
    .then(function(response) {
    // use response
})
jsx

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.

1
2
3
4
5
6
7
8
componentDidMount() {
    var self = this;
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then(function(response) {
        self.setState({ users: response.data });
      });
}
jsx

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.

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

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.

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

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

1
2
This is first one 
This 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.

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

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.

1
Expected corresponding JSX closing tag for 'br'.

Conclusion

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!

0