如何在React Hooks中使用shouldComponentUpdate? [英] How to use shouldComponentUpdate with React Hooks?

查看:3834
本文介绍了如何在React Hooks中使用shouldComponentUpdate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读以下链接:
https://reactjs.org/docs/hooks-faq .html#how-do-i-implement-shouldcomponentupdate
https://reactjs.org/blog/2018/10 /23/react-v-16-6.html

I've been reading these links:
https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-shouldcomponentupdate
https://reactjs.org/blog/2018/10/23/react-v-16-6.html

在第一个链接中显示为( https://reactjs .org/docs/hooks-faq.html#from-classes-to-hooks ):

In the first link it says (https://reactjs.org/docs/hooks-faq.html#from-classes-to-hooks):

shouldComponentUpdate:请参见React.memo

shouldComponentUpdate: See React.memo

第二个链接还指出:

使用PureComponent或shouldComponentUpdate输入相同的类组件时,它们可以避免渲染.现在,您可以通过将功能组件包装在React.memo中来对它们进行相同的操作.

Class components can bail out from rendering when their input props are the same using PureComponent or shouldComponentUpdate. Now you can do the same with function components by wrapping them in React.memo.

需要什么:

我希望Modal仅在可见Modal时呈现(由this.props.show管理)

I want Modal to render only when the Modal is visible (managed by this.props.show)

对于类组件:

shouldComponentUpdate(nextProps, nextState) {
    return nextProps.show !== this.props.show;
}

如何在功能组件中(在Modal.jsx中)使用memo?

How can I use memo instead in a functional component - here, in Modal.jsx?

相关代码:

功能组件Modal.jsx(我不知道如何检查props.show)

Functional component Modal.jsx (I don't know how to check for 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;

类组件PizzaMaker jsx呈现模态的部分:

The part of class component PizzaMaker jsx that renders 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>
    );

推荐答案

这是文档用于React.memo

您可以传递一个函数来控制比较:

You can pass a function to control the comparison :

const Modal = React.memo(
  props => {...},
  (prevProps, nextProps) => prevProps.show === nextProps.show
);

当函数返回true时,组件将不会重新渲染

when the function returns true, the component will not be re-rendered

这篇关于如何在React Hooks中使用shouldComponentUpdate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆