假设我有这些 React 组件:
const Compo1 = ({theName}) => {
return (
<Nested foo={() => console.log('Dr. ' + theName)}/>
);
};
const Compo2 = ({theName}) => {
function theFoo() {
console.log('Dr. ' + theName);
}
return (
<Nested foo={theFoo}/>
);
};
和嵌套组件,包裹在memo
:
const Nested = React.memo(({foo}) => {
return (
<Button onClick={foo}>Click me</Button>
);
});
传入的函数foo
总是被重新创建,Compo1
而且Compo2
,对吗?
如果是这样,既然foo
每次都接收到一个新的函数,是不是就意味着memo
没用了,所以Nested
总是会重新渲染?