React useEffect cleanup with current props

前端 未结 3 1474
野趣味
野趣味 2021-01-18 02:21

I ran into a need of cleaning a useEffect when component is unmounted but with an access to the current props. Like componentWillUnmount can do by getting this.props.whateve

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 02:36

    You need to track count in useEffect:

    import React, { useEffect } from "react";
    
    const B = ({ count }) => {
      useEffect(() => {
        return () => console.log(count);
      }, [count]);
    
      return 
    {count}
    ; }; export default B;

提交回复
热议问题