即使将备忘录设置为子组件,子组件仍然可以重新渲染。
这是一种我们将函数作为 props 传递给子组件的情况。
・src/example.js
import react, { usecallback, usestate } from "react";
import child from "./child";
const example = () => {
console.log("parent render");
const [counta, setcounta] = usestate(0);
const [countb, setcountb] = usestate(0);
const clickhandler = () => {
setcountb((pre) => pre + 1);
};
return (
<div classname="parent">
<div>
<h3>parent component</h3>
<div>
<button
onclick={() => {
setcounta((pre) => pre + 1);
}}
>
button a
</button>
<span>update parent component</span>
</div>
</div>
<div>
<p>the count of click button a:{counta}</p>
</div>
<child countb={countb} onclick={clickhandler} />
</div>
);
};
export default example;
・src/child.js
import { memo } from "react";
const childmemo = memo(({ countb, onclick }) => {
console.log("%cchild render", "color: red;");
return (
<div classname="child">
<h2>child component</h2>
<div>
<button onclick={onclick}>button b</button>
<span>uodate child component</span>
</div>
<span>the count of click button b :{countb}</span>
</div>
);
});
export default childmemo;
const clickhandler = () => {
setcountb((pre) => pre + 1);
};
<child countb={countb} onclick={clickhandler} />
const clickHandler = useCallback(() => {
setCountB((pre) => pre + 1);
}, []);


以上就是React Basics~Render Performance/ useCallback的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号