首页 > 解决方案 > 使用打字稿时react-google-login中的注销挂钩问题

问题描述

我对 onLogoutSuccess 有问题:

 const onLogoutSuccess = (res: any) =>{
    alert('Logout successfully');
  }

const { signOut } = useGoogleLogout({
    clientId,
    onLogoutSuccess,
    onFailure
  })

我得到错误:

(property) UseGoogleLogoutProps.onLogoutSuccess?: (() => void) | undefined
Type '(res: any) => void' is not assignable to type '() => void'.ts(2322)
index.d.ts(136, 12): The expected type comes from property 'onLogoutSuccess' which is declared here on type 'UseGoogleLogoutProps'

标签: javascriptreactjsgoogle-authentication

解决方案


我已经解决了这个问题,请看看我希望它对你的目标有所帮助。

JS代码

const onLogoutSuccess = () => {
    console.log('logout');
    history.push('/');
  };
  const onFailure = () => {
    console.log('logout fail');
  };
  const { signOut } = useGoogleLogout({
    clientId: GOOGLE_CLIENT_ID,
    onLogoutSuccess: onLogoutSuccess,
    onFailure: onFailure,
  });

html

 <button onClick={signOut}>Log Out</button>

推荐阅读