【发布时间】:2022-01-10 16:56:44
【问题描述】:
我在使用 nextjs 时遇到了 react memo 问题。在 _app 例如我有一个导入的按钮:
import { ChildComponent } from './ChildComponent';
export const Button = ({ classN }: { classN?: string }) => {
const [counter, setCounter] = useState(1);
const Parent = () => {
<button onClick={() => setCounter(counter + 1)}>Click me</button>
}
return (
<div>
{counter}
<Parent />
<ChildComponent />
</div>
);
};
子组件:
import React from 'react';
export const ChildComponent = React.memo(
() => {
React.useEffect(() => {
console.log('rerender child component');
}, []);
return <p>Prevent rerender</p>;
},
() => false
);
我做了一个在 React 中工作的程序在我自己的应用程序中无法解决: https://codesandbox.io/s/objective-goldwasser-83vb4?file=/src/ChildComponent.js
【问题讨论】:
标签: javascript reactjs next.js