React & Vue in the sameΒ app using Astro

yes it's possible withΒ Astro and ship πŸš€ with less code

Β·

3 min read

React & Vue in the sameΒ app using Astro

Astro is a new JavaScript bundler in the market. It follows the Bring Your Own Framework (BYOF) strategy and loads the components on-demand. Astro is still in the Beta version (v0.19). They're planning for a v1.0.0 release later this year. Keep out any eye πŸ‘€

Starting a new project in Astro is very easy, you just have to run this command npm init astro

# create a new project

mkdir js-is-one
cd js-is-one

# no other config is required, only this command is enough πŸ”₯
npm init astro -- --template framework-multiple

# then install the dependencies
npm i

# start the dev server
npm run dev

This command will load all the required config for developing an app using multiple frameworks - npm init astro --template framework-multiple

Recommended for best Astro dev experience

  1. Use VSCode text editor
  2. Use their Offical Astro VSCode extension

Folder structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚       └── ReactHW.jsx
β”‚       └── VueHW.vue
β”‚   └── pages/
β”‚       └── index.astro
β”œβ”€β”€ public/
β”œβ”€β”€ astro.config.mjs
└── package.json

astro.config.mjs

The template which we used framework-multiple loads all the frameworks [React, Vue, Preact, Svelte, Solid & Astro as well] by default and the Astro framework has a config file astro.config.mjs where all the frameworks can be configured. I will be removing other frameworks like Preact, Svelte and Solid and only keeping React & Vue

export default {
  renderers: [
    "@astrojs/renderer-react",
    "@astrojs/renderer-vue"
  ],
};

index.astro

---
// Component Imports
import * as react from '../components/ReactHW.jsx';
import VuewHW from '../components/VueHW.vue';

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />

        <link rel="icon" type="image/x-icon" href="/favicon.ico" />

    <style>
        :global(:root) {
            font-family: system-ui;
            padding: 2em 0;
        }
    </style>
  </head>
  <body>
      <main>
        <react.HW client:visible>
            <h1>Hello React!</h1>
        </react.HW>

        <VuewHW client:visible>
            <h1>Hello Vue!</h1>
        </VuewHW>
      </main>
  </body>
</html>

So both React & Vue are co-existing in the same file. Now I have a lot of question like

  1. using same CSS-in-JS components
  2. using the same linters like JSLint & Prettier
  3. Using the same plugin like lodash for both frameworks

Astro is all about partial hydration

Astro generates every website with zero client-side JavaScript, by default. Use any frontend UI component (React, Svelte, Vue, etc.) Astro will automatically render it to HTML at build-time and strip away all JavaScript.

Partial hydration means - In some scenarios where we will be needing client side JS to evaluate - like a autocomplete search bar, Add to cart button and many more. The act of only hydrating the individual components that require JavaScript and leaving the rest of your site as static HTML is called Partial hydration

The hydration is also in our control

<Component client:load />

This hydrates the component on page load.

<Component client:idle />

This hydrates the component as soon as main thread is free

<Component client:visible />

This hydrates the component as soon as the element enters the viewport (uses IntersectionObserver).

<Component client:only />

This hydrates the component at page load, similar to client:load.

DEV

The command npm run dev will load the app in the browser http://localhost:3000

Other frameworks using Astro

For a normal project with the framework of your choice, I'm listing the commands below πŸ‘‡

  1. React - npm init astro -- --template framework-react
  2. Vue - npm init astro -- --template framework-vue
  3. Svelte - npm init astro -- --template framework-svelte

Codesandbox Demo

Β