import React from "react";
import Taro from '@tarojs/taro';

/**
 * 页面显示, 隐藏 hook, 可以在 component 中使用
 */
export default (fn) => {
  const fnRef = React.useRef();
  fnRef.current = fn;

  const evt = React.useMemo(() => {
    const _evt = Taro.getCurrentInstance().router.onShow;
    Taro.eventCenter.on(_evt, () => fnRef.current());

    return _evt;
  }, []);

  React.useEffect(() => {
    return () => {
      Taro.eventCenter.off(evt);
    }
  }, []);
}
