javascript - 如何使用三元运算符在渲染函数中显示反应变量
问题描述
为什么不将“隐藏”添加到输入标签中?
class FontChooser extends React.Component {
constructor(props) {
super(props);
this.state = { hidden: true };
}
render() {
console.log(this.state.hidden);
return (
<div>
<input type="checkbox" id="boldCheckbox" {this.state ? 'hidden':'';} />
<button id="decreaseButton" hidden='true'>-</button>
<span id="fontSizeSpan" hidden="true">
{this.props.size}
</span>
<button id="increaseButton" hidden="true">
+
</button>
<span id="textSpan">{this.props.text}</span>
</div>
);
}
}
这是我的html:
<html>
<head>
<title>Font Chooser</title>
<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="FontChooser.js" type="text/jsx"></script>
</head>
<body>
<div id="container"></div>
<script type="text/jsx">
ReactDOM.render(
<div>
<FontChooser min='4' max='40' size='16' text='Fun with React!' bold='false'/>
</div>,
document.getElementById('container'))
;
</script>
</body>
</html>
解决方案
<input type={this.state.hidden ? 'hidden' : 'checkbox'} id="boldCheckbox" />
推荐阅读
- c++ - 是否/应该有一种方法来跟踪从智能指针获取的原始指针的有效性以用于调试/QA 目的?
- mysql - 如何将值从其他模式表插入 mysql 数据库?
- spring-boot - 删除方法显示“禁止”消息
- sql-server - ACCESS SQL 中的内连接 + 设置最大日期 + 命名
- c - configfs USB 小工具 write() 调用挂起
- matlab - 在matlab中用fsolve求解具有多个输入的非线性系统
- matlab - matlab归一化直方图
- javascript - 如何按类名查找 Chrome 上按钮的元素?
- python - 识别ndarray OpenALPR中的分段错误
- haskell - 堆栈构建找不到本地库