首页 > 解决方案 > React JS 道具没有更新

问题描述

在下面的代码中,当我更改 pendingCount 的状态时,第一个 key prop 发生了变化,但< Pending/>中的那个没有。这是为什么?

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

我还想补充一件事,那就是我想做的是在< Pending/>的 props 更改时重新渲染它。更改密钥道具是我在互联网上找到的建议方法之一,但它对我不起作用。那么对于功能组件有没有更好或首选的方法呢?

标签: reactjsreact-propsreact-functional-component

解决方案


你使用道具还是状态?

为了测试你尝试使用状态


推荐阅读