如何将 shouldComponentUpdate 与 React Hooks 一起使用?
How to use shouldComponentUpdate with React Hooks?
我一直在阅读这些 link:
https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-shouldcomponentupdate
https://reactjs.org/blog/2018/10/23/react-v-16-6.html
在第一个 link 中它说 (https://reactjs.org/docs/hooks-faq.html#from-classes-to-hooks):
shouldComponentUpdate: See React.memo
第二个link也指出:
Class 当输入属性相同时,组件可以使用 PureComponent 或 shouldComponentUpdate 从渲染中退出。现在,您可以通过将函数组件包装在 React.memo 中来对函数组件执行相同的操作。
想要什么:
我希望模态框仅在模态框可见时呈现(由 this.props.show 管理)
对于 class 组件:
shouldComponentUpdate(nextProps, nextState) {
return nextProps.show !== this.props.show;
}
如何在功能组件中使用 memo
- 在这里,在 Modal.jsx 中?
相关代码:
功能组件Modal.jsx(我不知道如何检查props.show)
import React, { useEffect } from 'react';
import styles from './Modal.module.css';
import BackDrop from '../BackDrop/BackDrop';
const Modal = React.memo(props => {
useEffect(() => console.log('it did update'));
return (
<React.Fragment>
<BackDrop show={props.show} clicked={props.modalClosed} />
<div
className={styles.Modal}
style={{
transform: props.show ? 'translateY(0)' : 'translateY(-100vh)',
opacity: props.show ? '1' : '0'
}}>
{props.children}
</div>
</React.Fragment>
);
});
export default Modal;
class 组件 PizzaMaker jsx 中渲染 Modal 的部分:
return (
<React.Fragment>
<Modal show={this.state.purchasing} modalClosed={this.purchaseCancel}>
<OrderSummary
ingredients={this.state.ingredients}
purchaseCancelled={this.purchaseCancel}
purchaseContinued={this.purchaseContinue}
price={this.state.totalPrice}
/>
</Modal>
...
</React.Fragment>
);
这里是documentation for React.memo
你可以传递一个函数来控制比较:
const Modal = React.memo(
props => {...},
(prevProps, nextProps) => prevProps.show === nextProps.show
);
当函数returnstrue
时,组件不会re-rendered
您也可以在导出语句中使用:
export default memo(Modal, (prevProps, nextProps) => prevProps.show === nextProps.show) ;
我一直在阅读这些 link:
https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-shouldcomponentupdate
https://reactjs.org/blog/2018/10/23/react-v-16-6.html
在第一个 link 中它说 (https://reactjs.org/docs/hooks-faq.html#from-classes-to-hooks):
shouldComponentUpdate: See React.memo
第二个link也指出:
Class 当输入属性相同时,组件可以使用 PureComponent 或 shouldComponentUpdate 从渲染中退出。现在,您可以通过将函数组件包装在 React.memo 中来对函数组件执行相同的操作。
想要什么:
我希望模态框仅在模态框可见时呈现(由 this.props.show 管理)
对于 class 组件:
shouldComponentUpdate(nextProps, nextState) {
return nextProps.show !== this.props.show;
}
如何在功能组件中使用 memo
- 在这里,在 Modal.jsx 中?
相关代码:
功能组件Modal.jsx(我不知道如何检查props.show)
import React, { useEffect } from 'react';
import styles from './Modal.module.css';
import BackDrop from '../BackDrop/BackDrop';
const Modal = React.memo(props => {
useEffect(() => console.log('it did update'));
return (
<React.Fragment>
<BackDrop show={props.show} clicked={props.modalClosed} />
<div
className={styles.Modal}
style={{
transform: props.show ? 'translateY(0)' : 'translateY(-100vh)',
opacity: props.show ? '1' : '0'
}}>
{props.children}
</div>
</React.Fragment>
);
});
export default Modal;
class 组件 PizzaMaker jsx 中渲染 Modal 的部分:
return (
<React.Fragment>
<Modal show={this.state.purchasing} modalClosed={this.purchaseCancel}>
<OrderSummary
ingredients={this.state.ingredients}
purchaseCancelled={this.purchaseCancel}
purchaseContinued={this.purchaseContinue}
price={this.state.totalPrice}
/>
</Modal>
...
</React.Fragment>
);
这里是documentation for React.memo
你可以传递一个函数来控制比较:
const Modal = React.memo(
props => {...},
(prevProps, nextProps) => prevProps.show === nextProps.show
);
当函数returnstrue
时,组件不会re-rendered
您也可以在导出语句中使用:
export default memo(Modal, (prevProps, nextProps) => prevProps.show === nextProps.show) ;