css ReactJS如何在改变值时渲染

pcww981p  于 2023-04-13  发布在  React
关注(0)|答案(1)|浏览(121)

when i click add to favourites(adding on local storage but not updating count, when i click button ref, updating count我这样做是为了我的电子商务项目,我有愿望清单.当我添加项目到我的愿望清单,我添加项目到我的localStorage.我需要显示有多少项目我添加到我的愿望清单.我不知道如何呈现头(自动)当改变值(localStorage).
我添加了按钮,与onClick我打电话给cou.当我添加项目到愿望清单什么也没发生(不渲染)当我点击按钮一切都很好.有人可以帮助请?一切都在标题组件(页面)

const [count, setCount] = useState((JSON.parse(localStorage.getItem("liked"))));

  const cou = () => {
    let oldData = JSON.parse(localStorage.getItem('liked') || "[]")
    if (oldData.length === count.length) {
      setCount((JSON.parse(localStorage.getItem("liked"))))
    } else {
      setCount((JSON.parse(localStorage.getItem("liked"))))
    }
  };

  useEffect(() => {
    let oldData = JSON.parse(localStorage.getItem('liked') || "[]")
    if (oldData.length === count.length) {
      setCount((JSON.parse(localStorage.getItem("liked"))))
    } else {
      setCount((JSON.parse(localStorage.getItem("liked"))))
    }
  }, [(count.length)]);

表头上的此跨度

<span style={{ position: 'relative', left: '-21px', top: '-18px' }}>{count.length}</span>}
gupuwyp2

gupuwyp21#

不要写[(count.length)],你可以用[count]检查吗?请让我知道输出。

相关问题