简体   繁体   English

在 React.js 中使用可选的 href 渲染“a”

[英]Rendering “a” with optional href in React.js

I need to render a table with a link in one of the columns, and searching for a most elegant way to do it.我需要在其中一列中呈现一个带有链接的表格,并寻找一种最优雅的方式来做到这一点。 My main problem is - not all table rows are supplied with that link.我的主要问题是 - 并非所有表格行都提供该链接。 If link is present - I need that "a" tag rendered.如果存在链接 - 我需要呈现“a”标签。 If not - no need for "a" tag at all.如果没有 - 根本不需要“a”标签。 Generally speaking I would like react to handle that choice (render vs not render) depending on this.state.一般来说,我想根据 this.state 做出反应来处理该选择(渲染与不渲染)。

This is what I have at the moment.这就是我目前所拥有的。

React.createClass({
    getInitialState: function () {
        return {
            pipeline: this.props.data.pipeline,
            liveUrl: this.props.data.liveUrl,
            posted: this.props.data.created,
            expires: this.props.data.end
        };
    },

    render: function () {
        return (
            <tr className="posting-list">
                <td><a href={this.state.liveUrl} target="_blank">{this.state.pipeline}</a></td>
                <td>Posted</td>
                <td>
                    <input className="datepicker" type="text" value={this.state.posted}/>
                </td>
                <td>
                    <input className="datepicker" type="text" value={this.state.expires}/>
                </td>
                <td>UPDATE, DELETE</td>
            </tr>
        );
    }
});

This results is DOM element :这个结果是 DOM 元素:

<a href="" target="_blank" data-reactid=".0.6.0.0.1:1.0.0">XING_batch</a>

This is not acceptable solution for me, because those blank hrefs are still clickable.这对我来说是不可接受的解决方案,因为那些空白的 href 仍然可以点击。 I also tried adding some logic to getInitalState( liveUrl: (this.props.data.liveUrl !== "") ? this.props.data.liveUrl : "javascript:void;", ), which worked fine, but looks weird, and adds errors in console( Uncaught SyntaxError: Unexpected token ; )我还尝试向 getInitalState( liveUrl: (this.props.data.liveUrl !== "") ? this.props.data.liveUrl : "javascript:void;", ) 添加一些逻辑,效果很好,但看起来很奇怪, 并在控制台中添加错误( Uncaught SyntaxError: Unexpected token ;

The only way I've got left is creating 2 different components for我剩下的唯一方法是创建 2 个不同的组件

It's just JavaScript, so you can use any logic you like, eg:它只是 JavaScript,所以你可以使用任何你喜欢的逻辑,例如:

<td>
    {this.state.liveUrl  
     ? <a ...>{this.state.pipeline}</a> 
     : this.state.pipeline}
</td>

You can choose the type of component at runtime, as well:您也可以在运行时选择组件的类型:

import * as React from "react";

const FooBar = props => {
  const Component = props.href ? "a" : "div";

  return (
    <Component href={href}>
      {props.children}
    </Component>
  );
};

<FooBar>Hello</FooBar> // <div>Hello</div>
<FooBar href="/">World</FooBar> // <a href="/">World</a>

Take a look at spread properties :看看 传播属性

You could use them like this for example:例如,您可以像这样使用它们:

var extras = { };
if (this.state.liveUrl) { extras.href = this.state.liveUrl; }
return <a {...extras} >My link</a>;

The values are merged with directly set properties.这些值与直接设置的属性合并。 If they're not on the object, they're excluded.如果它们不在对象上,则将它们排除在外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM