用usecontext在typeScript中使用
可以说,我有一个基本设置用于检查用户是否已登录:
import { createContext } from "react";
const UserContext = createContext<string | null>(null)
export default UserContext
在我的app.tsx中,我想创建一个usestate钩子,以便我可以在整个应用程序中管理上下文的状态:
//Context
const [context, setContext] = useState<string | null>(null)
<UserContext.Provider value={{context, setContext}}>
<Routes>
<Route path="/" element={<Home/>}/>
<Route path="/login" element={<Login/>}/>
<Route path="/register" element={<Register/>}/>
<Route path="/admin" element={<Admin/>}></Route>
</Routes>
</UserContext.Provider>
所以尽我所能看到我只需要登录用户的名称,或者如果我想拒绝访问某些页面,则将上下文的状态设置为null。现在的问题是,Typescript在这里对我大喊大叫,特别是在提供商的价值上:
Type '{ context: string | null; setContext:
React.Dispatch<React.SetStateAction<string | null>>; }' is not assignable to type 'string'.ts(2322)
我可以强迫以下值施放值:
value={{context, setContext} as any}
但这似乎不是一个特别优雅的“ typescripty”解决方案。
任何帮助都将受到赞赏!
Lets say I have a basic setup for checking whether a user is logged in or not:
import { createContext } from "react";
const UserContext = createContext<string | null>(null)
export default UserContext
In my App.tsx I want to create a useState hook so that I can manage the state of my context throughout my application:
//Context
const [context, setContext] = useState<string | null>(null)
<UserContext.Provider value={{context, setContext}}>
<Routes>
<Route path="/" element={<Home/>}/>
<Route path="/login" element={<Login/>}/>
<Route path="/register" element={<Register/>}/>
<Route path="/admin" element={<Admin/>}></Route>
</Routes>
</UserContext.Provider>
So as far as I can see I'll only ever need either the name of the logged in user, or set the state of the context to null if I want to deny access to certain pages. Now the issue is that typescript is yelling at me here, specifically in the value of the Provider:
Type '{ context: string | null; setContext:
React.Dispatch<React.SetStateAction<string | null>>; }' is not assignable to type 'string'.ts(2322)
I can force cast the value as follows:
value={{context, setContext} as any}
But that doesn't seem like a particularly elegant 'typescripty' solution.
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据亚历克斯的回答,我提出了一项对我有用的调整:
然后在app.tsx中:
Based on Alex's answer I came up with a tweak that works for me:
And then in App.tsx:
您已经将上下文键入:
但是,但是您为(近似)类型的上下文提供了一个值:
因此,如果您希望状态设置器在上下文中,那么它需要成为上下文类型的一部分:
You've typed your context as:
But then you provide a value for that context of (approxiamately) type:
So if you want the state setter in your context, then it needs to be part of the context's type:
如果要将
usestate
的数组作为返回值,这是一个正确的打字稿解决方案:提供商用法:
挂钩使用:
If you want to keep the
useState
's array as a return value, this is a correctly TypeScript solution:Provider usage:
Hook usage: