reactjs - 在我的反应钩子页面中单击 Google 登录按钮时,什么也没有发生
问题描述
单击Google Login
我的反应挂钩登录中的按钮时,显示以下错误。有人可以告诉这里可能是什么问题吗?以下是警告和错误详细信息
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
现在得到一个错误:
react-dom.development.js:3832 Uncaught Error: Expected `onClick` listener to be a function, instead got a value of `object` type.``
Check the render method of `Login`.
Login.js
import React, { useRef, useEffect, useState } from "react";
import { useGoogleLogin } from 'react-google-login';
import { refreshToken } from '../utils/refreshToken';
const clientId ="client_id_here";
const Login = () => {
const onSuccess = (res) =>{
console.log("Login successfully",res.profileObj);
refreshToken(res);
}
const onFailure = (res) => {
console.log('Login failed: res:', res);
alert(
`Failed to login.`
);
};
const {signIn, loaded} = useGoogleLogin ({
onSuccess,
onFailure,
clientId,
isSignedIn: true,
accessType: 'offline',
})
return (
<div className="App">
<h1>Login</h1>
<div className="inputForm">
<button onClick={()=>signIn}>
<img src="images/google.png" className="loginG"/>
<span className="loginText">Sign in</span>
</button>
</div>
</div>
)
}
export default Login;
src/utils/refreshToken.js
export const refreshTokenSetup = (res) => {
let refreshTiming = (res.tokenObj.expires_in || 3600 - 5 * 60) * 1000;
const refreshToken = async () => {
const newAuthRes = await res.reloadAuthResponse();
refreshTiming = (newAuthRes.expires_in || 3600 - 5 * 60) * 1000;
console.log('newAuthRes:', newAuthRes);
// saveUserToken(newAuthRes.access_token); <-- save new token
localStorage.setItem('authToken', newAuthRes.id_token);
setTimeout(refreshToken, refreshTiming);
};
setTimeout(refreshToken, refreshTiming);
};
解决方案
https://github.com/anthonyjgrove/react-google-login
根据文档,我猜您正在尝试使用useGoogleLogin
而不是GoogleLogin
?
检查此部分Login Hook
import { useGoogleLogin } from 'react-google-login'
const { signIn, loaded } = useGoogleLogin({
onSuccess,
...
});
问题是您将 React 组件(即GoogleLogin
)作为函数调用,它确实是一个函数,但只能由 React 本身调用。
推荐阅读
- rest - 如何在 Apache Flink 程序中调用 REST API
- django - Django ORM“过滤器”方法生成不带引号的 SQL 查询
- r - R 中的套索:cv.glmnet() 运行速度极慢
- javascript - Kendo UI 2019 返回 undefined in use requirejs JavaScript
- python - 更改 Django 的 TIME_ZONE 是否有任何负面影响?
- r - 按组计算连续天数
- sql - ORA-01722: 子查询上的数字无效
- memory - Protobuf 地图不断为 arena 分配新元素
- java - Spring Data REST - 在休息控制器的可分页参数中排序始终为空
- apache-spark - Apache Spark DStream 与 HDFS 序列文件中的静态数据记录连接的最佳方式是什么?