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('
版权声明:本文标题:reactjs - Multiple instances of components in a React monorepo with Vite and nested node apps - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1741639330a2389825.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论