react fetch data from api on button click

React Js- Fetch data from API on button click

In this post, we will discuss How to make an API fetch request on the button click Reactjs?. I’m assuming that you are clear about React Js from and about the React Js framework and you are also familiar with creating React Js applications. if you are not clear about these then please read the below post.

we will create a web page with textbox and HTML table list.we are binding the table with the data list returned by API.we want to filter the user list by user city so when we click the search button, we pass one parameter into a fetch API request.

In this post we will cover the following points:

  1. Fetch from external API function on button click.
  2. ReactJS display fetch response onClick of Button.
  3. How do make an api fetch request on button click Reactjs?

react fetch data from api on button click

Step 1 – Create React App

Step 2 – Create components for showing List data

Step 3 – Integrate Rest API and handle button click event


import React, { useState, useEffect } from "react";

const FetchDataOnButton = () => {
  const [userList, setusers] = useState(null);
  const [searchtxt, setSearch] = useState("")
  useEffect(() => {
  }, []);

  const getuesers = (url) => {
      .then((res) => res.json())
        (result) => {         
        (error) => {

  const handleValue = e => {

  const searchUser = e => {
  if (!userList) return <div>No Record Found</div>;
  return (
      <h2>Users List Funcational Component </h2>
      <div className="row">
        <div className="col-sm-6">
        onChange={e => handleValue(e)}
        <div className="col-sm-6">
        className='btn btn-primary btnsubmit'

      <table className="table">
            <th>User Id</th>
          { => (
export default FetchDataOnButton;

So on the button is clicked, we are making an HTTP request, Updating the state variables, and rendering the data.

We will discuss this in the next article, but make a note of the two-component types stateless functional components and stateful class components. Now that we have some understanding of the React components, let’s understand the App component in our Hello World react application
it is a JavaScript file. The class is named App and extends the component class from the React library, it contains a render method that returns some HTML.

You can have thousands of components. Facebook, I believe, has over 30000 components, is more complex, and the application has more a number of components. All right, then let me quickly summarize what we have learned about components

Ashok Patel

I'm a software engineer, having good experience in software programming web designing with great command on ASP.NET, React JS, Angular JS,.NET Core HTML5, JavaScript, T-SQL, JQuery.
Also have great experience in Electronics and electrical engineers design.
I like to do RND and Research.

Your Header Sidebar area is currently empty. Hurry up and add some widgets.