javascript - 有条件地渲染材质复选框
问题描述
我在我的 React 应用程序中使用 Material UI,它采用 JSON 数据来显示文本和其他数据。
{ "included": [{ "name": "someName", "price": "0", "required": true }] }
我创建了这个变量:它在控制台日志 const isRequired = useState(Included[0].required);
中返回。true
此 JSON 对象中的所有其他数据在我的Map
函数中呈现良好,但现在我想使用required
布尔值有条件地呈现复选框。
{Included.map((include) => {
console.log('includes', include);
return (
<div className="serviceWrapper" key={include.id}>
<FormControlLabel
value="end"
control={
!isRequired ? (
<Checkbox
color="primary"
onChange={() => {
setSomeData(!someData);
}}
/>
) : null
}
label={i18next.t('BOX.info')}
labelPlacement="End"
/>
<span className="price_right">
{include.price > 0 ? include.price : ''}
</span>
</div>
);
})}
Unhandled Rejection (TypeError): Cannot read property 'props' of null
但是,这会在 index.js 中返回错误。我不确定我是否可以按照我尝试的方式操作 Material UI Checkbox。还有其他方法可以做到这一点吗?
解决方案
根据文档, 的control
属性FormControlLabel
是必需的。(如果您将鼠标悬停在*
它旁边,工具提示会显示“必需”。)为其提供null
服务将不起作用。看来你必须把整个FormControlLabel
关掉。
推荐阅读
- node.js - 使用 Angular2 应用调用 NodeJs API
- python - 成对组合两个数组中的元素
- c# - 随机获取“System.Net.Sockets.SocketException:连接尝试失败,因为连接方
- java - 使用 Apache POI 替换表格单元格中文本的好方法
- javascript - CodeIgniter-选择客户并向他们展示信息时
- javascript - 组件渲染时加载组件 css
- php - 在给定数组中查找偶数
- python - CNN模型预测
- git - git show writing 100% but still show fatal: The remote end unexpectedly hang up and RPC failed 错误
- python-3.x - 我的 scrapy 脚本似乎没有调用 parse_item 方法