Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global variable in browser context is always undefined. I think you meant globalThis #91

Open
cpakken opened this issue Nov 24, 2021 · 4 comments

Comments

@cpakken
Copy link

cpakken commented Nov 24, 2021

const initialize = (storageKey, storageProvider, glbl = global) => {

The global variable will only exist in node during ssr. globalThis === window in browsers and globalThis === global in node

@haseeb5i
Copy link

haseeb5i commented Apr 2, 2022

yes, i am facing the same issue. As soon i use the hook, my app crashes with an error use-dark-mode.m.js:1 Uncaught ReferenceError: global is not defined. I am using vite with react 17

The error goes away if i put var global = globalThis; in my index.html file

@probablyraging
Copy link

I solved this by adding window.global = globalThis in the file where I create the entry point to the DOM

Example:

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';

window.global = globalThis;

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

@ttebify
Copy link

ttebify commented Mar 30, 2024

This will also work:

import useDarkMode from "use-dark-mode";
import { Switch } from "@nextui-org/react";
import { SVGProps } from "react";

export const ThemeSwitcher = () => {
    const darkMode = useDarkMode(false, {
        classNameDark: "dark",
        classNameLight: "light",
        global: window, // Just pass this as a config option
    });

    return (
        <Switch
            defaultSelected={darkMode.value}
            onValueChange={darkMode.toggle}
            size="lg"
            color="warning"
            startContent={<MoonIcon />}
            endContent={<SunIcon />}
        />
    );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants