作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试创建一个组件,当某个属性为真时,它不应该执行,但应该执行浅比较(PureComponent
的默认设置)。
我尝试过以下行为:
export default class ContentsListView extends PureComponent<Props> {
shouldComponentUpdate(props: Props) {
if (props.selecting) {
return false;
}
return super.shouldComponentUpdate(props);
}
render() {
}
}
但是,super.shouldComponentUpdate
没有定义。有什么方法可以在不编写自己的情况下“利用”PureComponent
的浅层比较吗?
最佳答案
当您使用 PureComponent
扩展您的组件时,您不应该实现 shouldComponentUpdate
。但是,如果您想使用 shouldComponentUpdate 功能,您可以简单地围绕原始组件编写一个包装器组件,或者使用 HOC 来实现您的自定义 shouldComponentUpdate
并呈现 PureComponent
示例代码
class ContentsListView extends PureComponent {
render() {
console.log("render list");
return (
...
);
}
}
export default class Wrapper extends React.Component {
shouldComponentUpdate(props) {
if (props.selecting) {
return false;
}
return true;
}
render() {
return <ContentsListView {...this.props} />;
}
}
您可以在 codesandbox here 上看到一个工作演示
关于javascript - 专门针对 PureComponent 的 shouldComponentUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48792262/
我有一个带有模板函数的基类,该函数具有通用模板类型和专用版本。 #ifndef BASE_CLASS #define BASE_CLASS #include using namespace std;
我有这个 3D vector 模板 template class Vec3TYPE{ public: union{ struct{ TYPE x,y,z; }; struct{ TY
我是一名优秀的程序员,十分优秀!