【问题标题】:render table row from array: react从数组中渲染表格行:反应
【发布时间】:2020-01-03 00:55:27
【问题描述】:

我正在尝试调用一个函数来使用 map 函数在我作为道具接收的文本数组上呈现表格行,但由于某种原因它们没有被呈现。我犯了句法错误吗?我已经验证了 license text props 数组不为空,只是返回一个字符串数组。

const showLicenseText = () => {
 return licenseText.map(text => {
      <tr>
        <ThPadding>
          License Information
          {':'}
        </ThPadding>
        <td>{text}</td>
      </tr>
  })
}

render方法中的原始代码是:

return (
  <table>
    <tbody>
      {bbbFileOpenDate && (
        <tr>
          <ThPadding>
            {text.bbbFileOpened}
            {':'}
          </ThPadding>
          <td>{bbbFileOpenDate}</td>
        </tr>
      )}
      {yearsInBusiness && !isOutOfBusiness && (
        <tr>
          <ThPadding>
            {yearsInBusinessLabel}
            {':'}
          </ThPadding>
          <td>{yearsInBusiness}</td>
        </tr>
      )}
      {businessStartDate && (
        <tr>
          <ThPadding>
            {businessStartDateLabel}
            {':'}
          </ThPadding>
          <td>{businessStartDate}</td>
        </tr>
      )}
      {showLicenseText}
      {locationStartDate && (
        <tr>
          <ThPadding>
            {locationStartDateLabel}
            {':'}
          </ThPadding>
          <td>{locationStartDate}</td>
        </tr>
      )} ....

【问题讨论】:

  • 也许用return ( &lt;tr&gt; etc..&lt;/tr&gt; )包围?
  • 不行不行

标签: javascript reactjs react-native jsx


【解决方案1】:

在您的map 中,您需要return 您所在的行。地图需要返回一些东西

const showLicenseText = () => {
 return licenseText.map(text => {
      return (<tr>
        <ThPadding>
          License Information
          {':'}
        </ThPadding>
        <td>{text}</td>
      </tr>)
  })
}

【讨论】:

    【解决方案2】:

    你忘了叫它!并且在地图中,由于您使用的是{},因此您需要明确的return 声明

      {showLicenseText()}
    

    另外我会修改如下:

    render之外定义函数

    showLicenseText = (licenseTextRef) => {
     return licenseTextRef.map(text => {
          return <tr>
            <ThPadding>
              License Information
              {':'}
            </ThPadding>
            <td>{text}</td>
          </tr>
      })
    }
    
    ==============================
    
      {this.showLicenseText(licenseText)}
    

    【讨论】:

    • @Ackman 你有什么错误吗?您的 render() 函数中是否定义了 showLicenseText
    • 是的,可能是这样。它是在里面定义的。我应该把它搬出去吗?
    • 做了但没有帮助。还是什么都没有。
    • 如果将其移出,则必须使用 this. 引用它
    猜你喜欢
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2020-03-26
    • 2016-03-23
    • 2015-06-09
    相关资源
    最近更新 更多