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

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

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

问题

我在Next.js中使用`createContext`来创建一个暗模式按钮以下是`ThemeContext.tsx`它最初是一个普通的`jsx`文件我正在尝试将其转换为TypeScript

```javascript
"use client";

import { useState, createContext } from "react";

type ThemeContextType = "light" | "dark";

interface ChildrenProps {
  children: React.ReactNode;
}

export const ThemeContext = createContext<ThemeContextType>("light");

export const ThemeProvider = ({ children }: ChildrenProps) => {
  const [mode, setMode] = useState<string>("dark");

  const toggle = (): void => {
    setMode((prev) => (prev === "dark" ? "light" : "dark"));
  };
  return (
    <ThemeContext.Provider value={{ toggle, mode }}>
      <div className={`theme ${mode}`}>{children}</div>
    </ThemeContext.Provider>
  );
};

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

Type '{ toggle: () => void; mode: string; }' is not assignable to type 'ThemeContextType'.ts(2322)

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.

&quot;use client&quot;;

import { useState, createContext } from &quot;react&quot;;

type ThemeContextType = &quot;light&quot; | &quot;dark&quot;;

interface ChildrenProps {
  children: React.ReactNode;
}

export const ThemeContext = createContext&lt;ThemeContextType&gt;(&quot;light&quot;);

export const ThemeProvider = ({ children }: ChildrenProps) =&gt; {
  const [mode, setMode] = useState&lt;string&gt;(&quot;dark&quot;);

  const toggle = (): void =&gt; {
    setMode((prev) =&gt; (prev === &quot;dark&quot; ? &quot;light&quot; : &quot;dark&quot;));
  };
  return (
    &lt;ThemeContext.Provider value={{ toggle, mode }}&gt;
      &lt;div className={`theme ${mode}`}&gt;{children}&lt;/div&gt;
    &lt;/ThemeContext.Provider&gt;
  );
};

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

Type &#39;{ toggle: () =&gt; void; mode: string; }&#39; is not assignable to type &#39;ThemeContextType&#39;.ts(2322)

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"的字符串。这样应该可以工作:

"use client";

import { useState, createContext } from "react";

type ModeType = "light" | "dark";
type ThemeContextType = {
  mode: ModeType;
  toggle: () => void;
};

interface ChildrenProps {
  children: React.ReactNode;
}

export const ThemeContext = createContext<ThemeContextType>({ mode: "light", toggle: () => {} });

export const ThemeProvider = ({ children }: ChildrenProps) => {
  const [mode, setMode] = useState<ModeType>("dark");

  const toggle = (): void => {
    setMode((prev) => (prev === "dark" ? "light" : "dark"));
  };

  return (
    <ThemeContext.Provider value={{ toggle, mode }}>
      <div className={`theme ${mode}`}>{children}</div>
    </ThemeContext.Provider>
  );
};
英文:

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:

&quot;use client&quot;;

import { useState, createContext } from &quot;react&quot;;

type ModeType = &quot;light&quot; | &quot;dark&quot;;
type ThemeContextType = {
  mode: ModeType;
  toggle: () =&gt; void;
};

interface ChildrenProps {
  children: React.ReactNode;
}

export const ThemeContext = createContext&lt;ThemeContextType&gt;({ mode: &quot;light&quot;, toggle: () =&gt; {} });

export const ThemeProvider = ({ children }: ChildrenProps) =&gt; {
  const [mode, setMode] = useState&lt;ModeType&gt;(&quot;dark&quot;);

  const toggle = (): void =&gt; {
    setMode((prev) =&gt; (prev === &quot;dark&quot; ? &quot;light&quot; : &quot;dark&quot;));
  };

  return (
    &lt;ThemeContext.Provider value={{ toggle, mode }}&gt;
      &lt;div className={`theme ${mode}`}&gt;{children}&lt;/div&gt;
    &lt;/ThemeContext.Provider&gt;
  );
};

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:

确定