react控制反转
一种减少子组件重渲的性能优化的手段
import { useState } from "react"
const Father = () => {
const [count, setCount] = useState(0);
console.log("Father rendered");
return (
<>
<p>I am Father p tag, {count}</p>
<button onClick={() => setCount(count + 1)}>Add Count</button>
<br />
<Children />
</>
);
};
const Children = () => {
console.log("Children rendered");
return <p>I am Children p tag</p>;
};
import { useState } from "react";
const Father = () => {
console.log("Father rendered");
return (
<FatherIoc>
<br />
<Children />
</FatherIoc>
);
};
const FatherIoc = ({ children }) => {
const [count, setCount] = useState(0);
return (
<>
<p>I am Father p tag, {count}</p>
<button onClick={() => setCount(count + 1)}>Add Count</button>
{children}
</>
);
};
const Children = () => {
console.log("Children rendered");
return <p>I am Children p tag</p>;
};
参考资料: https://juejin.cn/post/7146405884938158116#comment