[英]Argument of type '{...}' is not assignable to parameter of type 'Context<AuthProps>'
I'm creating a login and signup interface with Firebase, in my Ionic(React) app project, below are the snippet of my code.我正在使用 Firebase 创建登录和注册界面,在我的 Ionic(React) 应用项目中,下面是我的代码片段。
interface LoginProps extends RouteComponentProps<any> {}
interface AuthProps {}
const Login: FC<LoginProps> = ({ history }) => {
const handleLogin = useCallback(
async event => {
event.preventDefault();
const { email, password } = event.target.elements;
try {
await Firebase
.auth()
.signInWithEmailAndPassword(email.value, password.value);
history.push("/dashboard");
} catch (error) {
alert(error);
}
},
[history]
);
const { currentUser } = useContext<AuthProps>(AuthContext);
if (currentUser) {
return <Redirect to="/" />;
}
I got this error messages:我收到此错误消息:
[react-scripts] TypeScript error in /Users/matheuslira/Desktop/brasilsul-repasse/src/pages/Login.tsx(43,49):
[react-scripts] /Users/matheuslira/Desktop/brasilsul-repasse/src/pages/Login.tsx(43,49) 中的 TypeScript 错误:
[react-scripts] Argument of type '{ AuthContext: React.Context;
[react-scripts] '{ AuthContext: React.Context; 类型的参数AuthProvider: ({ children }: { children: any; }) => JSX.Element;
AuthProvider: ({ children }: { children: any; }) => JSX.Element; }' is not assignable to parameter of type 'Context'.
}' 不可分配给类型为 'Context' 的参数。
[react-scripts] Type '{ AuthContext: Context;
[反应脚本] 类型 '{ AuthContext: Context; AuthProvider: ({ children }: { children: any; }) => Element;
AuthProvider: ({ children }: { children: any; }) => Element; }' is missing the following properties from type 'Context': Provider, Consumer TS2345
}' 缺少类型 'Context' 中的以下属性:Provider、Consumer TS2345
the error message point to this part of the code:错误信息指向这部分代码:
const { currentUser } = useContext(AuthContext);
const { currentUser } = useContext(AuthContext);
I don't know how to fix this error.我不知道如何解决这个错误。
// Edit // 编辑
AuthContext Code授权上下文代码
import React, { useEffect, useState, createContext } from "react"
import Firebase from "./firebase"
export const AuthContext = createContext(null)
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null)
useEffect(() => {
Firebase.auth().onAuthStateChanged(setCurrentUser)
}, [])
return (<AuthContext.Provider value = {{currentUser}} >{children}</AuthContext.Provider>)
}
export default {
AuthContext,
AuthProvider
}
This is my solution and it seems to be working fine这是我的解决方案,它似乎工作正常
// context-fb.tsx
import React, { useEffect, useState, createContext } from "react";
import Firebase, { User } from "firebase";
export interface IAuthContext {
user: User;
}
const AuthContext = createContext<IAuthContext | null>(null);
const AuthProvider: React.FC = ({ children }) => {
const [currentUser, setCurrentUser] = useState<IAuthContext | null>(null);
useEffect(() => {
Firebase.auth().onAuthStateChanged((user: any) => {
console.log('state set', user)
setCurrentUser({ user });
});
}, []);
return (
<AuthContext.Provider value={currentUser}>{children}</AuthContext.Provider>
);
};
export default {
AuthContext,
AuthProvider
};
the basic parts of Login.tsx Login.tsx 的基本部分
import React, { FC, useCallback, useContext } from "react";
import { RouteComponentProps, Redirect } from "react-router";
import Firebase from "firebase";
import Stuff, { IAuthContext } from "../context-fb";
interface LoginProps extends RouteComponentProps<any> {}
const Login: FC<LoginProps> = ({ history }) => {
const handleLogin = useCallback(
async event => {
event.preventDefault();
const { email, password } = event.target.elements;
try {
await Firebase.auth().signInWithEmailAndPassword(
email.value,
password.value
);
history.push("/dashboard");
} catch (error) {
alert(error);
}
},
[history]
);
const authInfo = useContext<IAuthContext|null>(Stuff.AuthContext);
console.log("rendered login", authInfo?.user )
return authInfo?.user ? <Redirect to="/home" /> : <div></div>;
};
export default Login
and then my App.tsx然后我的 App.tsx
import Stuff from "./context-fb";
const App: React.FC = () => {
return <IonApp>
<IonReactRouter>
<Stuff.AuthProvider>
<IonRouterOutlet>
<Route path="/home" component={Home} exact={true} />
<Route path="/login" component={Login} exact={true} />
<Route exact path="/" render={() => <Redirect to="/login" />} />
</IonRouterOutlet>
</Stuff.AuthProvider>
</IonReactRouter>
</IonApp>
};
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.