小编frs*_*nmk的帖子

在 Typescript 中创建上下文 + 减速器时出错

这里我想创建一个AuthContext来将用户状态共享给其他组件。这里我使用 TypeScript 来设置变量类型。但是当我试图解决这个问题时,我遇到了很多错误。我对这个问题非常困惑。

这是我的AuthContext

import { createContext, ReactNode, useReducer } from 'react'
import { AuthReducer } from './Reducer';

export interface IState {
    isAuthenticated: boolean
    user: string | null
    token: string | null
}

// interface for action reducer
export interface IAction {
    type: 'LOGIN' | 'LOGOUT' | 'REGISTER' | 'FORGOT PASSWORD'
    payload?: any
}

interface IAuthProvider {
    children: ReactNode
}

const initialState = {
    isAuthenticated: false,
    user: '',
    token: ''
}

export const AuthContext = createContext<IState>(initialState); …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs use-reducer use-context

0
推荐指数
1
解决办法
1513
查看次数

标签 统计

reactjs ×1

typescript ×1

use-context ×1

use-reducer ×1