VITTHAL BHANDARI
VITTHAL BHANDARI

Reputation: 179

ReactJs : How to print data from console to web-page?

After a successful POST and GET query, the results of my operation are visible in the console of my React Dev Tools. How should I take those results, preferable create a table and render that table on my web-app itself?

Here is the GET request :

    axios.get('http://localhost:5000/result')
    .then((response) => {
      console.log(response);
        });
    }

The format of results displayed on console is like this : CONSOLE

enter image description here

Let's say I want to create a table by traversing through the results with headers _id and Name. I know I should use the map function. But exactly where and how should I do that in my code?

Upvotes: 0

Views: 14089

Answers (4)

Deen24ID
Deen24ID

Reputation: 361

Thanks to this page: https://www.skptricks.com/2019/02/can-you-consolelog-in-jsx.html.

You can do console.log in the render function of a class component or in the return statement of a function component. In your case:

function Foo(props){
  data = axios.get('http://localhost:5000/result')
  return(
    <>
      {console.log(data)}
    </>)
}

In my opinion, it's much more straight forward than the other state methods.

Upvotes: 0

Nithish
Nithish

Reputation: 6019

I have created a small sample app where I have used react-table for displaying the data as a table. Also In this example I have added promise in order to simulate server fetching data from server. This you can replace with actual server call i.e., axis.get etc.

React-table provides a lot many features that might be helpful. Such as you can provide which columns you wish to display out of all the columns from the data.

If you do not wish to use any library for displaying table, that is also possible only that you have to replace ReactTable with your custom table implementation.

Hope this helps.

Upvotes: 0

Bharanidhar Reddy
Bharanidhar Reddy

Reputation: 460

Assuming you know the concept of 'useState' in react hooks. If not please have an understanding of it.

Long story short, useState is used to store the data in variables.

const [data, setData] = useState();

Instead of console.log(response); you set the response to the variable i.e; setData(response);

In html,

<table>
//some headers, if needed

    //This will iterate your array of objects
    {data.map((eachData) => (
      <tr> <td>{eachData.id}</td>           
       <td>{eachData.name}</td>
       ....
      </tr>
      )
</table>

Please Note: The HTML part works for both class-based and function-based React components where as 'useState' is part of React hooks functionality, works only for function-based components.

Upvotes: 1

theswiss
theswiss

Reputation: 102

You have a few options. You can make your call in componentDidMount, set the result in a state and then render that state.

componentDidMount() {
   axios.get('http://localhost:5000/result')
    .then((response) => {
      this.setState({
        data: response // maninpulate your response here
      })
        });
    }
}

render() {
   const { data } = this.state;
   return this.renderTable(data) // this should return a table
}

Upvotes: 2

Related Questions