Use output from axios API in one function and pass to another

Sarvesh Agarwal

Hi Team any better way to get data from axios API and then use in the same function as data.

import React, { Component, useState, useMemo } from 'react';
import { useTable } from 'react-table';
import { COLUMNS } from './columns';
import axios from 'axios';


export const BasicTable = () => {
      const [myresponse, setmyresponse] =useState([]);
      axios.get('http://localhost:5001/inventory/cluster/cluster1/servertype/test1')
      .then((response) => {
        const myresponse= response.data;
        setmyresponse(myresponse)
      });

      const columns = useMemo(() => COLUMNS, [])
      const data = useMemo(() => myresponse,[])
      const tableInstance = useTable({
        columns: columns,
        data: data
      })
      
    const { getTableProps,getTableBodyProps, headerGroups,rows,prepareRow } =tableInstance
    
    return(
      <table {...getTableProps()}>
          <thead >
            {headerGroups.map(headerGroup => (
            <tr {...headerGroup.getHeaderGroupProps()}>
              {
                headerGroup.headers.map(column =>(
                  <th {...column.getHeaderProps()}>{column.render('Header')}</th>
                ))}
            </tr>
            ))}
          </thead>

          <tbody {...getTableBodyProps()}>
            {
              rows.map(row => {
                prepareRow(row)
                return (
                  <tr {...row.getRowProps()}>
                    {
                      row.cells.map(cell =>{
                        return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
                      })
                    }

                </tr>
                )
              })
            }

          </tbody>
      </table>
    )
};

What's happening right now, is i get data using console.log but the requests keep running indefinitely to my axios API. Any thoughts what am doing wrong? enter image description here

Surjeet Bhadauriya

Just do this: I have used useEffect so that your API gets called only once after your component did mount.

  useEffect(() => {
     axios
       .get('http://localhost:5001/inventory/cluster/cluster1/servertype/test1')   
       .then((response) => {
            const myresponse= response.data;
            setmyresponse(myresponse)
        });
   }, [])
    
   const columns = useMemo(() => COLUMNS, [])
   const data = useMemo(() => myresponse,[myresponse])
   const tableInstance = useTable({
      columns: columns,
      data: data
   });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a hook state from one component to another in a axios function?

From Dev

Pass output from one pipeline run and use as parameter in another pipeline

From Dev

I am unable to use pass variables from one function to another

From Dev

How to use itertools to pass the returned values from one function to another?

From Dev

Pass Terraform Output from One Module to Another

From Dev

Pass parameter from one Powershell function to another

From Dev

Get value from One function and pass it to another

From Dev

Pass value from one function to another to calculate with it

From Dev

How to pass a value from one function to another

From Dev

How to pass the one output from one process to another process in Nextflow?

From Dev

Pass output from a function as input in another function in Haskell

From Dev

Swift/iOS - How to use a value from one scope/function and pass it into another?

From Dev

How do I pass variables from one function to another and use them in conditional statements in python?

From Dev

Angular: Cannot pass output from one child component to another

From Dev

use info from one function to another

From Dev

Use values from one javascript function into another

From Dev

Pass a variable (id) from one function to another function

From Dev

Pass Dynamic Values from one function to another function in class

From Dev

I want to pass variables from one function to another function in codeigniter

From Dev

How to pass arguments from one function to another function in bash scripting?

From Dev

flask pass variable from one function to another function

From Dev

C Code: Pass string from one function to another function

From Dev

Get object array from axios and using return value call another axios function and append the output to first result

From Dev

How do you use the output of one function, as an inputted value in another?

From Dev

Sequelize use output of one function into another (date_trunc into literal)

From Dev

How to use output values of one function as input of another MATLAB

From Dev

How do I use the output of one axios request as a dependency for another when rendering components in React?

From Dev

Best Way to Pass Arguments from One Function to Another in Python

From Dev

Pass data from one fragmentation to another fragmentation function

Related Related

  1. 1

    How to pass a hook state from one component to another in a axios function?

  2. 2

    Pass output from one pipeline run and use as parameter in another pipeline

  3. 3

    I am unable to use pass variables from one function to another

  4. 4

    How to use itertools to pass the returned values from one function to another?

  5. 5

    Pass Terraform Output from One Module to Another

  6. 6

    Pass parameter from one Powershell function to another

  7. 7

    Get value from One function and pass it to another

  8. 8

    Pass value from one function to another to calculate with it

  9. 9

    How to pass a value from one function to another

  10. 10

    How to pass the one output from one process to another process in Nextflow?

  11. 11

    Pass output from a function as input in another function in Haskell

  12. 12

    Swift/iOS - How to use a value from one scope/function and pass it into another?

  13. 13

    How do I pass variables from one function to another and use them in conditional statements in python?

  14. 14

    Angular: Cannot pass output from one child component to another

  15. 15

    use info from one function to another

  16. 16

    Use values from one javascript function into another

  17. 17

    Pass a variable (id) from one function to another function

  18. 18

    Pass Dynamic Values from one function to another function in class

  19. 19

    I want to pass variables from one function to another function in codeigniter

  20. 20

    How to pass arguments from one function to another function in bash scripting?

  21. 21

    flask pass variable from one function to another function

  22. 22

    C Code: Pass string from one function to another function

  23. 23

    Get object array from axios and using return value call another axios function and append the output to first result

  24. 24

    How do you use the output of one function, as an inputted value in another?

  25. 25

    Sequelize use output of one function into another (date_trunc into literal)

  26. 26

    How to use output values of one function as input of another MATLAB

  27. 27

    How do I use the output of one axios request as a dependency for another when rendering components in React?

  28. 28

    Best Way to Pass Arguments from One Function to Another in Python

  29. 29

    Pass data from one fragmentation to another fragmentation function

HotTag

Archive