- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用例,其中有一个图像组件,该组件具有必需的“src”属性和可选的“link”属性,如下所示:
var Image = React.createClass({
propTypes: {
link: React.PropTypes.string,
event: React.PropTypes.object,
src: React.PropTypes.string.isRequired
},
handleClick: function(event, link) {
analytics.track(event)
.then(function() {
window.location = link;
});
},
render: function() {
return (
<img className='image' src={this.props.src} onClick={this.handleClick.bind(this, this.props.event, this.props.link)} />
);
} });
如果我想在调用图像组件时有选择地包含可选 Prop ,我该如何优雅地做到这一点?我最初的想法是做一个像这样的三元表达式,除了这不是有效的 JSX:
render: function() {
return (
<Image src={this.props.src} {this.props.link.hasOwnProperty('value') ? link=this.props.link.value : ''} />
)
}
在上面的示例中,“this.props.link”是一个对象,可能包含也可能不包含名为“value”的属性,其中包含单击图像时要浏览到的超链接。另外,如果不存在 link.value,我宁愿将其完全忽略,而不是简单地提供一个空字符串作为“link”属性的值。
我这样做的原因是,只有当 img 实际上链接到某个地方时,我才能在 Image 组件上添加 css“img:hover {cursor:pointer;}”,而不是全局设置它,这违反了我的 UX 规则应用程序。
我知道我可以简单地在三元组中渲染“link”属性,其中包含链接的值(如果存在),如果不存在则为空字符串,但出于好奇,我想看看是否存在也许是实现这一目标的另一种方式。
我还希望避免执行一堆创建大量冗余 JSX 代码的条件语句,如下所示:
render: function() {
if (this.props.link.hasOwnProperty('value')) {
return <Image link={this.props.link.value} src={this.props.src.value} />;
} else {
return <Image src={this.props.src.value} />;
}
.... // other optional properties
}
想象一下,如果你有很多可选的 Prop 想要放弃,那会变得多么失控......
最佳答案
看来你想多了。
<Image src={this.props.src} link={this.props.link.value} />
在您的组件中,您通常应该将任何虚假值视为省略。
if (this.props.link) {
...
}
数字是一个异常(exception),或者是罕见的(最好避免的情况),其中 bool 值默认为 true。
<小时/>更直接的答案是使用点差(0.12 中的新功能)。
var props = {src: this.props.src};
if (this.props.link.hasOwnProperty('value')) {
props.link = this.props.link.value;
}
<Image {...props} />
或
var extraProps = {};
if (this.props.link.hasOwnProperty('value')) {
extraProps.link = this.props.link.value;
}
<Image src={this.props.src} {...extraProps} />
关于reactjs - 在 React JSX 中选择性地渲染可选组件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769387/
根据这个: Selectivity is the value between 0 and 1, and it is the fraction of rows returned after applyi
我想知道C是否允许设置选择性#define。这就是我所说的选择性:如果我定义了一个结构: typedef struct mystruct{ int (*pointer_function)(struc
我正在尝试替换 pi与 math.pi使用以下 Python 函数。 def cleanup(x): return x.replace("pi", "math.pi") 我有以下字符串: a =
有没有一种方法可以只对类型声明可用的代码执行流程检查? 有一种方法可以启用每个文件的检查( header 中的 //@flow),但是一旦设置,代码的所有部分都需要类型声明(否则会记录错误,如“108
我在 openGL (openGl 1.1 win32) 中绘制了一个场景。 我使用 glClipPlane 隐藏前景对象以允许用户查看/编辑距离部分。选择是在 native 完成的,无需使用 ope
我正在开发允许第三方上传 HTML 片段的服务。在其中一些片段中,可能有指向 CSS 文件或内联 CSS 的链接。该服务有自己的 CSS 文件。 除了 iFrame 之外,还有什么方法可以让我指示特定
假设我有下面列出的用于通过 Web 服务将数据传递给客户端的类(类已简化): public class Customer { public int CustomerId { get; set;
我肯定有一个相当普遍的文档需求...... 我正在实现一个相当大的 Java 库代码库,除其他外,它包含各种类,这些类旨在在适当的抽象级别上公开给调用者/实现者。同时,代码库当然包含库的用户在使用 A
我有我的小 program.jar,它使用了巨大的 library.jar 的一小部分。 是否有一种工具可以将几个 jar 重新打包成一个,以便它可以独立运行并且尽可能小? 更新:大小很重要。 最佳答
如何为站点的某些部分强制使用 HTTPS,例如登录页面或注册页面,并为站点的其余部分使用 HTTP? 最佳答案 我最喜欢的强制转换为 https 的方法是将其作为您的 php 脚本中的第一件事。它适用
我一直在慢慢学习 Ruby(在这一点上,这可能是我投入大量时间实际学习的第一门语言)所以这对你们中的许多人来说可能是一个非常简单的问题。 我的学习玩具项目基本上是一个 roguelike。目前,我有一
我正在使用Liip Cache Control bundle处理项目中的缓存。通过使用此捆绑包,您可以像这样配置缓存: liip_cache_control: rules: -
在上个月的某个时候,一个随机网站决定在一个框架中为我公司的网站提供服务。忽略“他们在做什么?”的问题。一分钟,我使用了一些简单的 frame-buster Javascript: if (top.l
假设我有以下 Numpy 数组: array([[3, 5, 0], [7, 0, 2]]) 我现在想在值不为 0 的地方加 2。最快的方法是什么?我必须操纵相当大的多维数组? 最佳答案 在我看来:
我是 git 的新手,我正尝试在 Github 项目上进行协作。我 fork 了一个项目,添加了功能,并根据自己的需要移植到了 Android。添加的功能需要在基础项目中,而不是 android 相关
在以下两个命令中,第二个抛出异常说“未知编解码器 libfdk_aac”。谁能指出我,可能是什么问题? $> ffmpeg -loglevel verbose -re -i /var/mp4s/tes
我正在尝试 nvidia 的展开循环指令,但还没有找到有选择地打开它的方法。 假设我有这个... void testUnroll() { #pragma optionNV(unroll all
我们公司最近开始使用git-flow,我们遇到了以下问题: 我们有一个控制应用程序日志级别的 DEV_MODE bool 值,我们希望开发分支始终具有 DEV_MODE=true。 但是,在发布版本时
我试图在我的 DataFrame df 中删除 nan 值,但是我很难在不影响整行的情况下删除每一列。下面是我的 df 示例。 Advertising No Advertising nan
我已将播放服务更新到最新版本,目前为 9.2.0,我还想为谷歌播放服务使用选择性模块。 // compile 'com.google.android.gms:play-services:9.2.
我是一名优秀的程序员,十分优秀!