我正在使用Google Identity中的Sign In With Google按钮。我已经将button documentation page中的HTML放入React组件中。如下所示:
export default function GoogleLoginButton() {
return (
<>
<div
id="g_id_onload"
data-client_id="XXXXXX"
data-auto_prompt="false"
></div>
<div
className="g_id_signin"
data-type="standard"
data-size="large"
data-theme="outline"
data-text="sign_in_with"
data-shape="rectangular"
data-logo_alignment="left"
></div>
</>
);
}
在加载页面时,Google登录按钮第一次正确出现,我可以登录。登录按钮随后被注销按钮取代。问题是,当我点击注销按钮时,它应该再次呈现Google登录按钮,但它没有再次出现!这是为什么?
我可以补充说,在注销后刷新页面会带回谷歌按钮。
4条答案
按热度按时间zlhcx6iw1#
正如Stian所说,google script在应用程序渲染后会注入google button,问题是如果你重新渲染按钮所在的组件,它会消失,因为google script已经被执行过了,而且在未来的重新渲染中也不会被执行(它不会再注入google button)。
另一种解决方案是在useEffect中调用window.google.accounts.id.renderButton,useEffect应该被放置在被重新渲染的组件中。这将在每次调用useEffect时重新注入google按钮(组件被重新渲染)。
renderButton
方法接收到的第一个参数应该是一个div的ref,google脚本将在该div中注入登录按钮。注意:记住首先初始化google脚本调用google.accounts.id.initialize
下面是一个例子:
snz8szmq2#
它不起作用的原因是因为附带的client library在以后的渲染中不会再次运行。
在页面加载时,客户端库运行并在HTML所在的位置注入一个
iframe
。在稍后的渲染中,可以在DOM中看到这并没有发生;HTML存在,但没有iframe。一种解决方案是永远不要从DOM中删除HTML,而是在需要隐藏登录按钮时将样式
display: none
应用于它。11dmarpk3#
对于TypeScript,我稍微修改了Alex的答案。
首先,不要忘记将
<script src="https://accounts.google.com/gsi/client" async defer></script>
添加到react public 文件夹中的 index.html 页面。创建一个google_sso.ts文件并使用以下代码:
然后在任何需要按钮的地方,使用此按钮:
o8x7eapl4#
我找到了另一个解决这个问题的方法,即创建一个
useEffect
钩子,在组件每次呈现时将Google button<script>
标记插入DOM,这样可以防止在离开页面并返回时按钮消失,完整的React组件如下所示: