我已將一個類轉(zhuǎn)換為一個函數(shù),但是,“上下文”顯示為未定義。我希望使用 createContext() 從文件中獲取登錄變量(令牌、用戶 ID 等)。從 React 文檔來看,函數(shù)中沒有 contextType 的示例。它還將其列為“Class.contextType”,這表明它僅適用于類。除了轉(zhuǎn)換回課堂之外,還有其他解決方法嗎?// Importing moduleimport AuthContext from "../context/auth-context";// Bottom of page:export default AuthNew;AuthNew.contextType = AuthContext;// Snippet of code calling for this context.then(resData => { if (resData.data.login.token) { context.login( resData.data.login.token, resData.data.login.userId, resData.data.login.tokenExpiration ); } })預(yù)期結(jié)果應(yīng)該獲得這些 createContext() 變量,以便我的應(yīng)用程序可以登錄。
在 React Function 組件中未定義 contextType
慕哥6287543
2021-06-02 18:02:15