growi-react.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import type React from 'react';
  2. import type { GrowiFacade } from '@growi/core';
  3. declare global {
  4. interface Window {
  5. growiFacade: GrowiFacade;
  6. }
  7. }
  8. /**
  9. * Retrieves the React instance that this package should use.
  10. *
  11. * - **Production Mode**: Returns the React instance from `window.growiFacade.react`
  12. * to ensure a single shared React instance across the app.
  13. * - **Development Mode**: Returns the React instance passed as an argument,
  14. * which allows local development and hot reload without issues.
  15. *
  16. * @param react - The React instance to use during development
  17. * @returns A React instance to be used in the current environment
  18. *
  19. * @remarks
  20. * Using multiple React instances in a single app can cause serious issues,
  21. * especially with features like Hooks, which rely on a consistent internal state.
  22. * This function ensures that only one React instance is used in production
  23. * to avoid such problems.
  24. */
  25. export const growiReact = (react: typeof React): typeof React => {
  26. if (process.env.NODE_ENV === 'production') {
  27. return window.growiFacade.react as typeof React;
  28. }
  29. return react as typeof React;
  30. };