admin管理员组

文章数量:1299990

I have a monorepo Node solution with TS React apps, a Shared Components application, and my apps are built with Vite. This is the structure of my solution:

- root-app/
  - node_modules/
  - package.json
  - ...
  - shared-components/
    - node_modules/
    - package.json
    - Components
      - Context
      - GrommetWrapper
    - ...
  - webapp/
    - node_modules/
    - package.json
    - ...

I am using Vite to build my web apps, and I am encountering an issue where multiple instances of certain components, such as the app context and the Grommet theme, are being created. This is causing unexpected behavior and performance issues in my application.

Here is my EntryClient.tsx:

import ReactDOM from 'react-dom/client';
import { I18nextProvider, useSSR } from 'react-i18next';
import { BrowserRouter } from 'react-router-dom';
import i18n from './i18n';
import AppErrorBoundary from '../../Shared/Components/ErrorBoundary/ErrorBoundary';
import MainRouter from '../../Shared/Components/Router/MainRouter';
import AppContextProvider from '../../Shared/Components/Template/Context/AppContextProvider';
import GrommetWrapper from '../../Shared/Components/Template/GrommetWrapper/GrommetWrapper';
import { ICustomWindowWithData } from '../../Shared/Interfaces/ICustomWindow';

declare const window: ICustomWindowWithData;

// eslint-disable-next-line react-refresh/only-export-components
const App: React.FC = () => {
// @ts-expect-error this is a type error from the react-i18next library
  useSSR(window.__INITIAL_DATA__.i18nextInstance.store, window.__INITIAL_DATA__.context.Language || 'pt');
  return (
    <AppContextProvider context={window.__INITIAL_DATA__.context}>
      <I18nextProvider i18n={i18n}>
        <BrowserRouter>
          <GrommetWrapper>
            <AppErrorBoundary>
              <MainRouter />
            </AppErrorBoundary>
          </GrommetWrapper>
        </BrowserRouter>
      </I18nextProvider>
    </AppContextProvider>
  );
};

ReactDOM.hydrateRoot(document.getElementById('root')!, <App />);

This is my vite.config.ts:

import {
  AliasOptions,
  CommonServerOptions, ConfigEnv, UserConfig, defineConfig, loadEnv,
} from 'vite';
import react from '@vitejs/plugin-react';
import { cjsInterop } from 'vite-plugin-cjs-interop';
import fs from 'node:fs';
import path from 'node:path';

// /config/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default defineConfig(({ command, mode, isSsrBuild, isPreview }: ConfigEnv) => {
  console.log('

本文标签: reactjsMultiple instances of components in a React monorepo with Vite and nested node appsStack Overflow