【发布时间】:2019-01-25 15:59:42
【问题描述】:
假设我有这些 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 中传递的函数是always recreated 在Compo1 和Compo2,对吗?
如果是这样,既然foo每次都会收到一个新的函数,是不是意味着memo将无用,因此Nested总是会被重新渲染?
【问题讨论】:
标签: javascript reactjs memo