javascript - 如何在表td标签中的循环中显示 react 变量值

标签 javascript html reactjs axios

我使用axios来获取值,现在我想在循环中显示td标签中的值如何执行它。

我的代码是:

let apiUrl = "http://api_url_here";
axios.get(apiUrl, {headers: headers})
  .then((response) => {
    console.log(response.data[0].provider.firstName);
    //console.log(token);[0]
  })

我的td代码是:

<tr>
  <td className="font-weight-medium"> <img className="logo" src={jessica} alt="pam-logo" /> </td>
  <td>jessicajames@gmail.com </td>
  <td>(012)-876789876</td>
  <td>Family Physician</td>
  <td>23145655</td>
  <td>Mountain view,Ave</td>
  <td><Icon icon={editIcon} width="20px"/>&nbsp;&nbsp;<Icon icon={deleteIcon} width="20px"/></td>
</tr>

我需要第一个 td 中的response.data[0].provider.firstName,并且相应地所有内容都相同,并且它应该在循环中以获得下一次迭代。

最佳答案

试试这个。

import React, {useState, useEffect} from 'react'

const myFunction = () => {
  const [ data, setData ] = useState(null)

  const apiUrl = "http://api_url_here"

  useEffect(() => {
    axios.get(apiUrl, {headers: headers})
      .then((response) => {
         setData(response.data)
      })
  }, [])


  return (
    <>
      {
        data && data.map((item, index) => (
          <tr key={index}>
            <td>{item.provider.firstName}</td>
            ...
          </tr>
        ))
      }
    </>
  )
}

关于javascript - 如何在表td标签中的循环中显示 react 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299189/

相关文章:

javascript - Google Analytics 事件跟踪表选择

javascript - 如何在 JavaScript 中使用双引号定义图像 src

html - 为什么有两种样式元素样式,有优势吗?

reactjs - 带有 withStyles 的 JSX 元素中的通用类型参数

Javascript - 将日期(以毫秒为单位)转换为可读时间 - 缺少最后 3 位数字

javascript - 为什么 'secret' 变量不是全局变量?

javascript - 如何在 React Native 中提交值后关闭弹出窗口?

reactjs - 如何在makeStyles中使用 'theme'和 'props'?

jquery - 悬停在 IE 中无法正常工作

php - 如何调整菜单图标的尺寸