React useMount 组件挂载时执行一次

useMount

//utils/index.js
export const useMount = (callback) => {
    
    
    useEffect(() => {
    
    
        callback()
    }, [])
}

使用

import {
    
      useMount } from 'utils';
useMount(() => {
    
    
  console.log('组件挂载时执行一次 ');
  // fetch(`${apiUrl}/users`).then(async response => {
    
    
  //   console.log('response2', response);
  //   if (response.ok) {
    
    
  //     setUsers(await response.json());
  //   }
  // });
});

猜你喜欢

转载自blog.csdn.net/weixin_43245095/article/details/125928584