React JS Props 不更新
React JS Props not updating
在下面的代码中,当我更改 pendingCount 的状态时,第一个关键道具会发生变化,但 中的那个不会。这是为什么?
<div>
<motion.p
key={pendingCount}
whileHover={{textDecoration:"underline"}}
style={{cursor:"pointer"}}
onClick={ ()=>{ render(<Pending key={pendingCount} data={AllPendingsData}/>) } }
className="position-relative d-inline-block pe-3"
>
Pending Requests
<span className="badge rounded-pill bg-dark ms-2">
{pendingCount}
</span>
</motion.p>
</div>
我还想补充一件事,那就是我想做的是在 属性发生变化时重新渲染它。更改关键道具是我在互联网上找到的建议方法之一,但它对我不起作用。那么对于功能组件,有没有更好或更喜欢的方法呢?
你用道具还是状态?
为了测试你尝试使用状态
问题出在 Drew Reese 提到的 onClick 上。
道具正在引起更新,但仅当用户单击该元素时。
因此,我没有将数据作为 prop 传递,而是使用 useContext 钩子解决了我的问题。
在下面的代码中,当我更改 pendingCount 的状态时,第一个关键道具会发生变化,但
<div>
<motion.p
key={pendingCount}
whileHover={{textDecoration:"underline"}}
style={{cursor:"pointer"}}
onClick={ ()=>{ render(<Pending key={pendingCount} data={AllPendingsData}/>) } }
className="position-relative d-inline-block pe-3"
>
Pending Requests
<span className="badge rounded-pill bg-dark ms-2">
{pendingCount}
</span>
</motion.p>
</div>
我还想补充一件事,那就是我想做的是在
你用道具还是状态?
为了测试你尝试使用状态
问题出在 Drew Reese 提到的 onClick 上。 道具正在引起更新,但仅当用户单击该元素时。 因此,我没有将数据作为 prop 传递,而是使用 useContext 钩子解决了我的问题。