简体   繁体   中英

How to make useEffect react on props change as fast as ComponentDidUpdate?

rotateLogo() fires immediately when isLoading prop gets updated in the following case:

class Logo extends Component {
    constructor(props) {
      super(props);

      };
    }

    componentDidUpdate() {
      if (this.props.isLoading) {
        this.rotateLogo();
      }

    }

...

However, when trying to implement the same in a functional component with useEffect, there is a considerable delay between the update of isLoading and the activation of rotateLogo()

const Logo = ({ isLoading }) => {

  useEffect(() => {
    if (isLoading) {
      rotateLogo();
    }
  }, [isLoading])

...

在这种情况下,您可能希望同步重新渲染,因此您应该使用useLayoutEffect而不是useEffecthttps : useEffect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM