假设我有这些 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
总是要重新渲染?