Context API with Styled Components Error: setTheme is not a function

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

Context API with Styled Components Error: setTheme is not a function

问题

以下是您要翻译的内容:

在首次使用上下文 API 并尝试使用 styled-components 实现应用程序中的暗模式时,尝试切换主题时出现错误 setTheme 未定义。

App.js

import { ThemeContext, ThemeProviderWrapper } from "./context/ThemeContext";
import { ThemeProvider } from "styled-components";
import { lightTheme, darkTheme } from "./theme";
import Home from "./pages/Home";
import GlobalStyles from "./styles/Global.styled";
import { Outlet } from "react-router-dom";
import { useContext } from "react";

const App = () => {
  const { theme } = useContext(ThemeContext);

  return (
    <ThemeProviderWrapper>
      <ThemeProvider theme={theme === "light" ? lightTheme : darkTheme}>
        <Home />
        <Outlet />
        <GlobalStyles />
      </ThemeProvider>
    </ThemeProviderWrapper>
  );
};

export default App;

在上下文文件中,我有以下内容:

import React, { useState, createContext } from "react";

export const ThemeContext = createContext({
  theme: "light",
  setTheme: () => {},
});

export const ThemeProviderWrapper = ({ children }) => {
  const [theme, setTheme] = useState("light");

  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      {children}
    </ThemeContext.Provider>
  );
};

而在我尝试切换主题的组件中,我有以下内容:

import {
  Navbar,
  StyledFontAwesomeIcon,
  ToggleSwitch,
  ToggleSwitchText,
  Title,
} from "../styles/Navbar.styled";
import { faMoon } from "@fortawesome/free-regular-svg-icons";
import { useContext } from "react";
import { ThemeContext } from "styled-components";

const Header = () => {
  const { theme, setTheme } = useContext(ThemeContext);

  const handleClick = () => {
    setTheme(theme === "light" ? "dark" : "light");
  };
  return (
    <Navbar>
      <Title>Where in the world?</Title>
      <ToggleSwitch onClick={handleClick}>
        <StyledFontAwesomeIcon icon={faMoon} />
        <ToggleSwitchText>Dark Mode</ToggleSwitchText>
      </ToggleSwitch>
    </Navbar>
  );
};

export default Header;
英文:

I am using context API for the first time and trying to implement dark mode in an application using styled components. When trying to toggle the theme, I get the error setTheme is not defined.

App.js

import { ThemeContext, ThemeProviderWrapper } from &quot;./context/ThemeContext&quot;;
import { ThemeProvider } from &quot;styled-components&quot;;
import { lightTheme, darkTheme } from &quot;./theme&quot;;
import Home from &quot;./pages/Home&quot;;
import GlobalStyles from &quot;./styles/Global.styled&quot;;
import { Outlet } from &quot;react-router-dom&quot;;
import { useContext } from &quot;react&quot;;

const App = () =&gt; {
  const { theme } = useContext(ThemeContext);

  return (
    &lt;ThemeProviderWrapper&gt;
      &lt;ThemeProvider theme={theme === &quot;light&quot; ? lightTheme : darkTheme}&gt;
        &lt;Home /&gt;
        &lt;Outlet /&gt;
        &lt;GlobalStyles /&gt;
      &lt;/ThemeProvider&gt;
    &lt;/ThemeProviderWrapper&gt;
  );
};

export default App;

In the context file I have this:

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

export const ThemeContext = createContext({
  theme: &quot;light&quot;,
  setTheme: () =&gt; {},
});

export const ThemeProviderWrapper = ({ children }) =&gt; {
  const [theme, setTheme] = useState(&quot;light&quot;);

  return (
    &lt;ThemeContext.Provider value={{ theme, setTheme }}&gt;
      {children}
    &lt;/ThemeContext.Provider&gt;
  );
};

 

And in the component where I'm attempting to theme switch, I have this:

import {
  Navbar,
  StyledFontAwesomeIcon,
  ToggleSwitch,
  ToggleSwitchText,
  Title,
} from &quot;../styles/Navbar.styled&quot;;
import { faMoon } from &quot;@fortawesome/free-regular-svg-icons&quot;;
import { useContext } from &quot;react&quot;;
import { ThemeContext } from &quot;styled-components&quot;;

const Header = () =&gt; {
  const { theme, setTheme } = useContext(ThemeContext);

  const handleClick = () =&gt; {
    setTheme(theme === &quot;light&quot; ? &quot;dark&quot; : &quot;light&quot;);
  };
  return (
    &lt;Navbar&gt;
      &lt;Title&gt;Where in the world?&lt;/Title&gt;
      &lt;ToggleSwitch onClick={handleClick}&gt;
        &lt;StyledFontAwesomeIcon icon={faMoon} /&gt;
        &lt;ToggleSwitchText&gt;Dark Mode&lt;/ToggleSwitchText&gt;
      &lt;/ToggleSwitch&gt;
    &lt;/Navbar&gt;
  );
};

export default Header;

答案1

得分: 1

Your ThemeContext import is wrong in the component where you are attempting this. Rather than importing from 'styled-components', import it from your context provider file.

import { ThemeContext } from '你主题上下文提供者的路径'
英文:

Your ThemeContext import is wrong in the component where you are attempting this.Rather than importing from 'styled-components' import it from your context provider file.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { ThemeContext } from &#39;path of your theme context provider&#39;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 22:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243126.html
匿名

发表评论

匿名网友

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

确定