[英]React.memo not working on a functional component
I tried using React.memo() to prevent re-rendering on one of my components but it keeps on re-rendering whenever there are changes on its Parent Component .我尝试使用React.memo()来防止在我的一个组件上重新渲染,但只要其Parent Component发生更改,它就会继续重新渲染。
Here's my child component with React.memo()这是我的带有 React.memo() 的子组件
const Transactions = React.memo(() => {
console.log('rerender')
return (
<></>
)
})
Here's where it's being called.这是它被调用的地方。 Whenever I click the Add button it calls the state function setModal(true), Transactions component is being re-rendered
每当我单击“添加”按钮时,它都会调用 state function setModal(true),正在重新渲染事务组件
const ScholarHistory = ({setModal}) => {
return (
<div className="pml-scholar pml-scholar--column">
<div className="pml-scholar-container">
<button onClick={() => setModal(true)} className="pml-scholar-add-button"> Add </button>
</div>
<Transactions />
</div>
)
}
I am new to React and been exploring/creating wider projects.我是 React 新手,一直在探索/创建更广泛的项目。 Any ideas why it keeps on re rendering?
任何想法为什么它会继续重新渲染? Thanks!
谢谢!
I tried to remove the setModal prop on the ScholarHistory Component and was able to prevent the re-rendering.我试图删除 ScholarHistory 组件上的setModal道具,并且能够防止重新渲染。
I am not sure what causes it as it has no relation at all to the child component other than being a prop to the parent component - ScholarHistory .我不确定是什么原因导致它,因为它与子组件完全没有关系,除了作为父组件的道具 - ScholarHistory 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.