最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to share React context between _document.js and pages on Next.js? - Stack Overflow

programmeradmin0浏览0评论

Let's say I have this context:

export const ThemeContext = createContext();

export function ThemeWrapper({ children }) {
  const sharedState = {
    darkMode: false,
  };

  return (
    <ThemeContext.Provider value={sharedState}>
      {children}
    </ThemeContext.Provider>
  );
}

export function useThemeContext() {
  return useContext(ThemeContext);
}

Which I can access on _document.js like this:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { ThemeWrapper, ThemeContext } from "../context/theme";

class MyDocument extends Document {
  static contextType = ThemeContext;
  render() {
    console.log("theme", this.context);
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

class Wrapped extends Document {
  render() {
    return (
      <ThemeWrapper>
        <MyDocument />
      </ThemeWrapper>
    );
  }
}

export default Wrapped;

Now I also want to access this context from a page:

import { useThemeContext } from "../context/theme";

const SomePage = () => {
  const theme = useThemeContext();

  console.log("theme", theme);

  return (
    <div>Hi, I'm a page</div>
  );
};

_document.js logs out theme { darkMode: false } on the Next.js console when the page is first loaded but SomePage logs out theme undefined on the Chrome console everytime you navigate to it.

Any suggestions?

I need to toggle some class on the html tag depending on this context. Trying to manually toggle dark mode using Tailwind CSS.

Let's say I have this context:

export const ThemeContext = createContext();

export function ThemeWrapper({ children }) {
  const sharedState = {
    darkMode: false,
  };

  return (
    <ThemeContext.Provider value={sharedState}>
      {children}
    </ThemeContext.Provider>
  );
}

export function useThemeContext() {
  return useContext(ThemeContext);
}

Which I can access on _document.js like this:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { ThemeWrapper, ThemeContext } from "../context/theme";

class MyDocument extends Document {
  static contextType = ThemeContext;
  render() {
    console.log("theme", this.context);
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

class Wrapped extends Document {
  render() {
    return (
      <ThemeWrapper>
        <MyDocument />
      </ThemeWrapper>
    );
  }
}

export default Wrapped;

Now I also want to access this context from a page:

import { useThemeContext } from "../context/theme";

const SomePage = () => {
  const theme = useThemeContext();

  console.log("theme", theme);

  return (
    <div>Hi, I'm a page</div>
  );
};

_document.js logs out theme { darkMode: false } on the Next.js console when the page is first loaded but SomePage logs out theme undefined on the Chrome console everytime you navigate to it.

Any suggestions?

I need to toggle some class on the html tag depending on this context. Trying to manually toggle dark mode using Tailwind CSS.

Share Improve this question edited Feb 12, 2021 at 20:10 Camilo asked Feb 12, 2021 at 14:07 CamiloCamilo 7,2245 gold badges45 silver badges66 bronze badges 8
  • So is the new page wrapped inside a ThemeWrapper? – Keith Commented Feb 12, 2021 at 14:13
  • Instead of wrapping the document try wrapping Main in ThemeWrapper. When you wrap the native document from next, I have a feeling that might not work. – Mellet Commented Feb 12, 2021 at 14:13
  • You can also try creating a custom app and adding your context wrapper there. nextjs/docs/advanced-features/custom-app – Mellet Commented Feb 12, 2021 at 14:17
  • 2 You should use the ThemeWrapper in a custom App not Document – nip Commented Feb 12, 2021 at 14:19
  • @Keith Nope, do I need to? I thought that wrapping the document would give me access anywhere down the tree. – Camilo Commented Feb 12, 2021 at 14:35
 |  Show 3 more ments

1 Answer 1

Reset to default 6 +50

Wrapping _document with ThemeWrapper doesn't give you access to the context inside pages (probably because it's only rendered in the server), you will need to wrap _app for that. Just note that _document will not re-render on context state changes.

For this specific use case, an alternative is to use next-themes.

发布评论

评论列表(0)

  1. 暂无评论