javascript - 如何根据对象的修改在对象中显示数字?
问题描述
我正在尝试减少一个数字并在减少时实时显示它。
const counts = {"2200": 1, "2230": 1, "2300": 3}
const displayTimes = availableTimes.map((item) => {
if (timeExists) {
//should show number as it is decreasing, but will only show default, first number set
return <span> {counts[`${item.time}`]} </span>
} else {
return (
<li onClick={ () => counts[`${item.time}`] > 1 && counts[`${item.time}`]-- }> //decreases count by 1.
{`${item.time}`}
</li>
)
}
}
return (
<div>
<ul>{displayTimes}</ul>
</div>
)
减少确实有效,我已经测试过了;显示减少的数字不会。
我尝试在这个函数中使用 useState,但 React 不允许我这样做。我什至不确定我需要为此使用 useState ...
解决方案
推荐阅读
- android - 片段更新文本更改
- time-series - 在thingboard中单击自定义时间序列小部件的行时如何在对话框中获取并显示特定数据键的值
- r - RStudio 错误:将网络位置添加到 RStudio 中的 .libPath
- python-3.x - 错误:flask_appbuilder.security.sqla.manager:尝试使用 gunicorn 启动应用程序时数据库创建和初始化失败
- c# - 类型转换如何从双字节转换为字节
- amazon-web-services - Create Infrastructure Documentation from terraform + gitlab-ci system
- python - 无法获取正确的 div 以从表中获取数据
- javascript - Javascript - 切换到另一个选项卡时暂停视频(在同一页面上)
- canvas - Trying to allow user to choose which chart to display (using chart.js)
- python-3.x - InlineKeyboardMarkup Telegram bot (pyTelegramBotAPI)