Next.js
This library was originally designed to work with Next.js, so integrating it is beautifully simple.
Install the script
react-color-mode
provides a <script>
tag that needs to be injected as high as possible into your document (preferably in the <head>
). This script allows us to setup the color mode before the rest of the page is loaded, preventing the page from flickering on page load. It needs to be installed in your custom document (Learn more about custom documents at https://nextjs.org/docs/advanced-features/custom-document):
Install the Context Provider
<ColorModeContextProvider>
provides a React.Context
from which you can access and update the current color mode. The easiest wayto use this provider in a Next.js app is by installing it in your custom app (Learn more about custom apps at https://nextjs.org/docs/advanced-features/custom-app):
Use and update the color mode
Once you've installed the <ColorModeContextProvider>
, accessing and updating the color mode becomes simple using the useColorMode
hook:
Last updated