Type ‘…’ is not assignable to type ‘…’ when using a context.

huangapple go评论116阅读模式
英文:

Type '...' is not assignable to type '...' when using a context

问题

  1. 我在Next.js中使用`createContext`来创建一个暗模式按钮以下是`ThemeContext.tsx`它最初是一个普通的`jsx`文件我正在尝试将其转换为TypeScript
  2. ```javascript
  3. "use client";
  4. import { useState, createContext } from "react";
  5. type ThemeContextType = "light" | "dark";
  6. interface ChildrenProps {
  7. children: React.ReactNode;
  8. }
  9. export const ThemeContext = createContext<ThemeContextType>("light");
  10. export const ThemeProvider = ({ children }: ChildrenProps) => {
  11. const [mode, setMode] = useState<string>("dark");
  12. const toggle = (): void => {
  13. setMode((prev) => (prev === "dark" ? "light" : "dark"));
  14. };
  15. return (
  16. <ThemeContext.Provider value={{ toggle, mode }}>
  17. <div className={`theme ${mode}`}>{children}</div>
  18. </ThemeContext.Provider>
  19. );
  20. };

我从ThemeContext.Providervalue中得到了错误。

  1. Type '{ toggle: () => void; mode: string; }' is not assignable to type 'ThemeContextType'.ts(2322)
  2. index.d.ts(329, 9): The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes & ProviderProps<ThemeContextType>'
英文:

I am using createContext in Next.js to create a dark mode button; here's the ThemeContext.tsx, it's originally a normal jsx file; I am trying to convert it to TypeScript.

  1. &quot;use client&quot;;
  2. import { useState, createContext } from &quot;react&quot;;
  3. type ThemeContextType = &quot;light&quot; | &quot;dark&quot;;
  4. interface ChildrenProps {
  5. children: React.ReactNode;
  6. }
  7. export const ThemeContext = createContext&lt;ThemeContextType&gt;(&quot;light&quot;);
  8. export const ThemeProvider = ({ children }: ChildrenProps) =&gt; {
  9. const [mode, setMode] = useState&lt;string&gt;(&quot;dark&quot;);
  10. const toggle = (): void =&gt; {
  11. setMode((prev) =&gt; (prev === &quot;dark&quot; ? &quot;light&quot; : &quot;dark&quot;));
  12. };
  13. return (
  14. &lt;ThemeContext.Provider value={{ toggle, mode }}&gt;
  15. &lt;div className={`theme ${mode}`}&gt;{children}&lt;/div&gt;
  16. &lt;/ThemeContext.Provider&gt;
  17. );
  18. };

I got the error from the value in ThemeContext.Provider value={{toggle, mode}}&gt;

  1. Type &#39;{ toggle: () =&gt; void; mode: string; }&#39; is not assignable to type &#39;ThemeContextType&#39;.ts(2322)
  2. index.d.ts(329, 9): The expected type comes from property &#39;value&#39; which is declared here on type &#39;IntrinsicAttributes &amp; ProviderProps&lt;ThemeContextType&gt;&#39;

答案1

得分: 1

你需要更新ThemeContextType,因为现在它使得提供者的value只能接受等于"light"或"dark"的字符串。这样应该可以工作:

  1. "use client";
  2. import { useState, createContext } from "react";
  3. type ModeType = "light" | "dark";
  4. type ThemeContextType = {
  5. mode: ModeType;
  6. toggle: () => void;
  7. };
  8. interface ChildrenProps {
  9. children: React.ReactNode;
  10. }
  11. export const ThemeContext = createContext<ThemeContextType>({ mode: "light", toggle: () => {} });
  12. export const ThemeProvider = ({ children }: ChildrenProps) => {
  13. const [mode, setMode] = useState<ModeType>("dark");
  14. const toggle = (): void => {
  15. setMode((prev) => (prev === "dark" ? "light" : "dark"));
  16. };
  17. return (
  18. <ThemeContext.Provider value={{ toggle, mode }}>
  19. <div className={`theme ${mode}`}>{children}</div>
  20. </ThemeContext.Provider>
  21. );
  22. };
英文:

You need to update ThemeContextType, because, as of now, it makes it so the value of the provider can only accept a string equal to "light" or "dark". This should work:

  1. &quot;use client&quot;;
  2. import { useState, createContext } from &quot;react&quot;;
  3. type ModeType = &quot;light&quot; | &quot;dark&quot;;
  4. type ThemeContextType = {
  5. mode: ModeType;
  6. toggle: () =&gt; void;
  7. };
  8. interface ChildrenProps {
  9. children: React.ReactNode;
  10. }
  11. export const ThemeContext = createContext&lt;ThemeContextType&gt;({ mode: &quot;light&quot;, toggle: () =&gt; {} });
  12. export const ThemeProvider = ({ children }: ChildrenProps) =&gt; {
  13. const [mode, setMode] = useState&lt;ModeType&gt;(&quot;dark&quot;);
  14. const toggle = (): void =&gt; {
  15. setMode((prev) =&gt; (prev === &quot;dark&quot; ? &quot;light&quot; : &quot;dark&quot;));
  16. };
  17. return (
  18. &lt;ThemeContext.Provider value={{ toggle, mode }}&gt;
  19. &lt;div className={`theme ${mode}`}&gt;{children}&lt;/div&gt;
  20. &lt;/ThemeContext.Provider&gt;
  21. );
  22. };

huangapple
  • 本文由 发表于 2023年6月6日 17:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定