gpt4 book ai didi

javascript - 调用render方法时ReactJS是 "clever"吗?

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:13 24 4
gpt4 key购买 nike

此组件的渲染方法确实使用提供给组件的任何 Prop 。

组件是否会在 props 发生变化时重新渲染?

class MyComponent extends React.Component {
constructor(props) {
super(props);
const { propValue } = props;
// do something with propValue...
}
render () {
return (
<div>foo</div>
);
}
}

最佳答案

render 是否会被调用 - 是的。除非您实现 shouldComponentUpdate 以返回 false

DOM 是否会被重新渲染 - 不会。

您可能还想看看 https://babeljs.io/docs/plugins/transform-react-constant-elements/将静态元素提升。

In

const Hr = () => {
return <hr className="hr" />;
};

Out

const _ref = <hr className="hr" />;

const Hr = () => {
return _ref;
};

关于javascript - 调用render方法时ReactJS是 "clever"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45712278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com