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

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

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

问题

以下是您要翻译的内容:

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

App.js

  1. import { ThemeContext, ThemeProviderWrapper } from "./context/ThemeContext";
  2. import { ThemeProvider } from "styled-components";
  3. import { lightTheme, darkTheme } from "./theme";
  4. import Home from "./pages/Home";
  5. import GlobalStyles from "./styles/Global.styled";
  6. import { Outlet } from "react-router-dom";
  7. import { useContext } from "react";
  8. const App = () => {
  9. const { theme } = useContext(ThemeContext);
  10. return (
  11. <ThemeProviderWrapper>
  12. <ThemeProvider theme={theme === "light" ? lightTheme : darkTheme}>
  13. <Home />
  14. <Outlet />
  15. <GlobalStyles />
  16. </ThemeProvider>
  17. </ThemeProviderWrapper>
  18. );
  19. };
  20. export default App;

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

  1. import React, { useState, createContext } from "react";
  2. export const ThemeContext = createContext({
  3. theme: "light",
  4. setTheme: () => {},
  5. });
  6. export const ThemeProviderWrapper = ({ children }) => {
  7. const [theme, setTheme] = useState("light");
  8. return (
  9. <ThemeContext.Provider value={{ theme, setTheme }}>
  10. {children}
  11. </ThemeContext.Provider>
  12. );
  13. };

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

  1. import {
  2. Navbar,
  3. StyledFontAwesomeIcon,
  4. ToggleSwitch,
  5. ToggleSwitchText,
  6. Title,
  7. } from "../styles/Navbar.styled";
  8. import { faMoon } from "@fortawesome/free-regular-svg-icons";
  9. import { useContext } from "react";
  10. import { ThemeContext } from "styled-components";
  11. const Header = () => {
  12. const { theme, setTheme } = useContext(ThemeContext);
  13. const handleClick = () => {
  14. setTheme(theme === "light" ? "dark" : "light");
  15. };
  16. return (
  17. <Navbar>
  18. <Title>Where in the world?</Title>
  19. <ToggleSwitch onClick={handleClick}>
  20. <StyledFontAwesomeIcon icon={faMoon} />
  21. <ToggleSwitchText>Dark Mode</ToggleSwitchText>
  22. </ToggleSwitch>
  23. </Navbar>
  24. );
  25. };
  26. 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

  1. import { ThemeContext, ThemeProviderWrapper } from &quot;./context/ThemeContext&quot;;
  2. import { ThemeProvider } from &quot;styled-components&quot;;
  3. import { lightTheme, darkTheme } from &quot;./theme&quot;;
  4. import Home from &quot;./pages/Home&quot;;
  5. import GlobalStyles from &quot;./styles/Global.styled&quot;;
  6. import { Outlet } from &quot;react-router-dom&quot;;
  7. import { useContext } from &quot;react&quot;;
  8. const App = () =&gt; {
  9. const { theme } = useContext(ThemeContext);
  10. return (
  11. &lt;ThemeProviderWrapper&gt;
  12. &lt;ThemeProvider theme={theme === &quot;light&quot; ? lightTheme : darkTheme}&gt;
  13. &lt;Home /&gt;
  14. &lt;Outlet /&gt;
  15. &lt;GlobalStyles /&gt;
  16. &lt;/ThemeProvider&gt;
  17. &lt;/ThemeProviderWrapper&gt;
  18. );
  19. };
  20. export default App;

In the context file I have this:

  1. import React, { useState, createContext } from &quot;react&quot;;
  2. export const ThemeContext = createContext({
  3. theme: &quot;light&quot;,
  4. setTheme: () =&gt; {},
  5. });
  6. export const ThemeProviderWrapper = ({ children }) =&gt; {
  7. const [theme, setTheme] = useState(&quot;light&quot;);
  8. return (
  9. &lt;ThemeContext.Provider value={{ theme, setTheme }}&gt;
  10. {children}
  11. &lt;/ThemeContext.Provider&gt;
  12. );
  13. };

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

  1. import {
  2. Navbar,
  3. StyledFontAwesomeIcon,
  4. ToggleSwitch,
  5. ToggleSwitchText,
  6. Title,
  7. } from &quot;../styles/Navbar.styled&quot;;
  8. import { faMoon } from &quot;@fortawesome/free-regular-svg-icons&quot;;
  9. import { useContext } from &quot;react&quot;;
  10. import { ThemeContext } from &quot;styled-components&quot;;
  11. const Header = () =&gt; {
  12. const { theme, setTheme } = useContext(ThemeContext);
  13. const handleClick = () =&gt; {
  14. setTheme(theme === &quot;light&quot; ? &quot;dark&quot; : &quot;light&quot;);
  15. };
  16. return (
  17. &lt;Navbar&gt;
  18. &lt;Title&gt;Where in the world?&lt;/Title&gt;
  19. &lt;ToggleSwitch onClick={handleClick}&gt;
  20. &lt;StyledFontAwesomeIcon icon={faMoon} /&gt;
  21. &lt;ToggleSwitchText&gt;Dark Mode&lt;/ToggleSwitchText&gt;
  22. &lt;/ToggleSwitch&gt;
  23. &lt;/Navbar&gt;
  24. );
  25. };
  26. 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.

  1. 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 -->

  1. 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:

确定