logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Commands
      File Conventions
      src/
      App.[tj]sx
      entry.[tj]s
      entry.server.[tj]sx
      modern.runtime.[tj]s
      routes/
      *.[server|node].[tj]sx
      api/
      lambda/*.[tj]s
      server/
      modern.server.[tj]s
      shared/
      config/
      html/
      favicon.*
      icon.*
      mock/
      public/
      upload/
      modern.config.[tj]s
      Runtime
      Core
      createRoot
      render
      Router
      router
      SSR
      NoSSR
      renderStreaming
      renderString
      createRequestHandler
      BFF
      useHonoContext
      Utility
      CSS-In-JS API
      Head
      loadable
      📝 Edit this page
      Previous pageApp.[tj]sxNext pageentry.server.[tj]sx

      #entry.[tj]sx

      Normally, the routes/ and App.[tj]sx hook files can meet our needs. When we need to add custom behavior before component rendering or take full control of the webpack packaging entry, we can create entry.[tj]s file in the src or entry directory. Here are two cases for discussion。

      #Add custom behavior before Render

      This is implemented in src/entry.[tj]s as follows:

      src/entry.tsx
      import { createRoot } from '@modern-js/runtime/react';
      import { render } from '@modern-js/runtime/browser';
      
      const ModernRoot = createRoot();
      
      async function beforeRender() {
        // todo
      }
      
      beforeRender().then(() => {
        render(<ModernRoot />);
      });

      #Take full control of the webpack entry

      When the project does not install the @modern-js/runtime dependency, src/entry.[tj]sx? is the real webpack packaging entry file, and you can directly organize the code like using create-react-app and other scaffolds:

      src/entry.jsx
      import React from 'react';
      import ReactDOM from 'react-dom/client';
      import App from './App';
      
      ReactDOM.createRoot(document.getElementById('root')!).render(<App />);