【问题标题】:Material-UI including an inline link with the label for Radio Buttons, checkboxes, etcMaterial-UI 包括带有单选按钮、复选框等标签的内联链接
【发布时间】:2018-02-28 21:45:42
【问题描述】:

我正在尝试在 React 应用程序中包含与 Material-UI 单选按钮列表标签内联的动态链接。当我实现以下代码时,我看到[object Object] 代替了我希望显示参考文档链接的位置。我做错了什么?

referenceDocsLink(protocol) {
  return (
      <a
        className="Content-Documentation-Button"
        href={`${BASE_DOCS_LINK}/reference/${protocol}/`}
        rel="noopener noreferrer"
        target="_blank"
      >
        Reference Docs
      </a>
  );
}

<RadioButtonGroup name="connectionProtocol" >
  <RadioButton
    key="Content-Protocol-RadioButton-http"
    label={`HTTPS Device API ${this.referenceDocsLink('http')}`}
    name="protocol-http"
    value="http"
  />
  <RadioButton
    key="Content-Protocol-RadioButton-mqtt"
    label={`MQTT ${this.referenceDocsLink('mqtt')}`}
    name="protocol-mqtt"
    value="mqtt"
  />
</RadioButtonGroup>

UI 渲染如下:

【问题讨论】:

  • 这是因为 label 属性只接受一个字符串。所以它不会渲染你插入的 JSX,而是渲染 [object Object] 因为实际上它正在获取一个 React.createElement 对象。

标签: reactjs material-ui


【解决方案1】:

您可以尝试将 JSX 插入 label 属性而不是字符串:

<RadioButton
    key="Content-Protocol-RadioButton-http"
    label={<div>HTTPS Device API {this.referenceDocsLink('http')}</div>}
    name="protocol-http"
    value="http"
  />

但这一切都取决于RadioButton 是否接受 JSX 作为 prop 值类型。

【讨论】:

  • 看起来它确实接受 JSX。在您的示例中添加(将&lt;div&gt; 切换为&lt;span&gt;),它就像一个魅力。谢谢!!
  • 我正在动态生成 RadioButtons。尽管链接看起来非常好,但是当我尝试单击它们时,除了选择单选按钮之外什么都没有发生。我正在使用material-ui v0.21。有什么想法吗?
猜你喜欢
  • 2019-12-08
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2014-08-13
  • 1970-01-01
相关资源
最近更新 更多