首页 > 解决方案 > 如何根据对象的修改在对象中显示数字?

问题描述

如有必要,整个组件都在这个 github url 上

我正在尝试减少一个数字并在减少时实时显示它。

  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 ...

标签: javascriptreactjs

解决方案


推荐阅读