当我想使用自定义挂钩从 useContext 获取数据时出现错误

问题描述 投票:0回答:1

我收到此错误:

导出的变量“useAuth”具有或正在使用外部模块“c:/Users/PC/Desktop/my Practices/react-course-part2-starter/src/state-management/Contexts/authContext”中的名称“AuthcontextType”,但不能被命名.ts(4023)
const useAuth: () => AuthcontextType

当我想运行此文件中的代码时:

import { useContext } from "react";
import AuthContext from "../Contexts/authContext";

const useAuth = () =>  useContext(AuthContext);

export default useAuth

这是我的

AuthContext
文件:

import React, { Dispatch } from "react";
import { authAction } from "../Reducers/authReducer";

interface AuthcontextType {
    user:string,
    dispatch:Dispatch<authAction>
}

const AuthContext= React.createContext<AuthcontextType> ({} as AuthcontextType)

export default AuthContext

有人知道我该如何解决这个问题吗?

reactjs react-hooks react-context state-management react-state-management
1个回答
0
投票

我认为你也必须导入 authcontexttype 。

import AuthContext, { AuthcontextType } from "../Contexts/authContext";
© www.soinside.com 2019 - 2024. All rights reserved.