Intro to Mitosis: The common reactive transformer

Mitosis.js is a compiler software that consumes a common part syntax and outputs framework-specific code. Which means you’ll be able to write software performance as soon as and generate it to React, Svelte, or Angular, amongst others. What distinguishes Mitosis from different compile-time frameworks is its “write as soon as, run wherever” method to compilation. Mitosis is a powerful engineering feat, and it has purposes wherever you should summary front-end frameworks into pluggable parts. 

The hidden advantage of Mitosis is its revelation of the widespread points and unity in front-end JavaScript frameworks. It is a new mannequin that might yield sudden insights and new instructions for future JavaScript growth.

What’s Mitosis.js?

Mitosis is a undertaking from the parents at, who additionally developed envelope-stretching tasks just like the Qwik.js framework and Partytown. Builder is itself instance of the sort of software that advantages from Mitosis. Briefly, Builder means that you can visually design UI layouts throughout numerous underlying framework implementations. It wants a standard language to course of and output these numerous frameworks, and that language is Mitosis.

As of this writing, Mitosis helps the next front-end JavaScript frameworks:

Mitosis additionally helps outputting to straight HTML and has Qwik.js on the roadmap. Additionally, Mitosis is the interpretation bridge that Builder makes use of between third-party design software Figma. That’s to say, the abstraction layer is helpful in taking design output and remodeling it into the specified goal framework.

Mitosis is syntactically a subset of JSX, or JavaScript XML. This is sensible for a few causes. JSX is the syntax in React, the commonest and influential JavaScript framework. Additionally, on the finish of the day, JSX is a reasonably good distillation of the weather of a reactive UI descriptor. Particularly, Mitosis makes use of a JSX variant impressed by the one utilized in Stable.js.

Itemizing 1 is a straightforward instance that reveals off a couple of conventions of Mitosis’s JSX variant.

Itemizing 1. Primary listing output in Mitosis (TypeScript)

import  useStore  from '';

sort Props = 
  author: string;

export default perform SongList(props: Props) 
  const state = useStore(
    songs: [
       title: "Strawberry Fields", writer: "John Lennon" ,
       title: "Penny Lane", writer: "Paul McCartney" ,
       title: "Dark Horse", writer: "George Harrison" ,
       title: "It don't come Easy", writer: "Ringo Starr" 
  return (
      <For every=state.songs>(music, index) => <div>music.title</div></For>

Itemizing 1 takes an inventory of objects (songs) and outputs a property from every one (music.title). There are few issues to notice on this pattern. First, the file exports a default perform. Due to this fact, it’s defining a purposeful part. Mitosis will rework this part to the correct construction for its goal framework. 

Subsequent, notice that the part makes use of a hook, useStore. This hook works analogously to the one present in React. The code then makes use of the state to iterate over the songs with a <For> part. Iterating over collections is a kind of areas of variety in frameworks and the <For> part affords a easy, unified strategy to categorical it.

Additionally, observe the usual dealing with of part properties, through the props argument to the perform (with its attendant TypeScript sort definition of Props).

Run the compiler

To place this part via the Mitosis compiler (or, strictly talking, transpiler), we will arrange a easy Node Package deal Supervisor (NPM) undertaking. To start out, provoke a undertaking (npm init), then set up the Mitosis libraries by getting into

npm set up

The Mitosis compiler will routinely discover the information we need to compile based mostly on a mitosis.config.js file. We’ll use the easy one proven in Itemizing 2.

Itemizing 2. Mitosis.config.js

module.exports = 
  information: 'src/**',
  targets: ['vue3', 'solid', 'svelte', 'react', 'angular'],

Itemizing 2 tells the place the sources are to be discovered (src/**) and what output frameworks to make use of.

Our part is in TypeScript, so we’ll want a easy tsconfig.json file, as properly:

Itemizing 3. tsconfig.js

    "jsx": "protect",
    "jsxImportSource": ""

Itemizing 3 tells the TypeScript command-line interface (tsc-cli) the right way to deal with the JSX it encounters. On this case, it leaves the syntax as-is (protect) and defines the module to make use of for import ( See the JSX overview and code pattern within the TypeScript documentation for particulars.

Mitosis with React

Now we’re able to get some output. Run npm exec mitosis construct. This may drop information into the output/ listing, one department for every goal framework. Let’s take a peek on the /output/react/src/parts/Songs.jsx model, which is able to look one thing like Itemizing 4.

Itemizing 4. React model of Songs.jsx

import * as React from "react";
import  useState  from "react";
perform SongList(props) 
  const [songs, setSongs] = useState(() => [
      title: "Strawberry Fields",  writer: "John Lennon"
      title: "Penny Lane",  writer: "Paul McCartney"
      title: "Dark Horse",  writer: "George Harrison"
      title: "It don't come Easy",  writer: "Ringo Starr"
  return /* @__PURE__ */ React.createElement("div", null, songs == null ? void 0 :, index) => /* @__PURE__ */ React.createElement("div", null, music.title)));

  SongList as default

So, we will see that Mitosis has switched to utilizing the React implementation of useState and has opted for utilizing React.createElement to outline the div and to iterate over the gathering. It exports the part as a default module. This appears to be like like legitimate React thus far, however let’s test it. 

We will go to a different listing and spin up a create-react-app actual fast (see the Create React App web page for particulars), then go to the brand new listing that has simply been created. Within the /src listing, we’ll copy over the output/react/src/parts/Songs.jsx file from our Mitosis undertaking. We open App.jsx and import the brand new part by including import “./Songs.jsx” as Songs, then go into the template markup and use the part someplace with <Songs />

Now, we will run the app with npm begin. Verify the output at localhost:3000 and also you’ll see the listing of music names on the web page.

Good. Now we all know that Mitosis is working with React. In a real-world scenario, we may readily construct a pipeline so as to add Mitosis to our construct course of. 

Mitosis with Svelte

Let’s use a fast shortcut to see how Mitosis works with Svelte. Copy the contents of /output/svelte/src/parts/Songs.svelte (noticing that Mitosis has given the correct extension to the file). Go to the Svelte playground and paste the supply into the left-hand code panel. After a second, you will notice the music listing on the correct facet of the display screen. 

Mitosis is producing right Svelte. When you’re curious, Itemizing 5 reveals the idiomatic Svelte iteration for the <For> part.

Itemizing 5. Tune iterator in Svelte

#every songs as music, index
And Vue, Angular, SolidJS

You may take comparable steps to confirm the correctness of every of the opposite output targets. 

Configuration and plugins

Mitosis is meant to be fairly versatile. Particularly, the Mitosis playground demonstrates the flexibility to vary a configuration to pick not solely totally different frameworks however totally different traits inside them. As an example, you’ll be able to decide a state supplier in React, selecting between useState, Mobx, and Stable. You can also choose totally different styling options, like Emotion CSS, Styled Parts, and Styled JSX.

Mitosis additionally helps the flexibility to outline plugins that run arbitrary code at strategic moments, like earlier than and after the underlying JSON knowledge construction is generated.

Consuming framework code

You would possibly surprise whether it is potential to flip Mitosis’s performance from producing to consuming framework code. For example, may we take a UI outlined in a framework implementation and parse it into the Mitosis JSON mannequin? That may not solely allow us to two-directionally translate between Mitosis and a framework however truly translate between totally different frameworks through the Mitosis mannequin. 

I requested’s founder, Steve Sewell whether or not Mitosis may perceive framework code. Right here’s what he mentioned:

[Framework parsing] is certainly the most important request we get. Proper now most frameworks are a bit too freeform (not sufficient constraints) to do that reliably. That mentioned Svelte is one of the best candidate for that, which is actively being labored on, we name it sveltosis.


Mitosis is at the moment nonetheless in beta. That being mentioned, it has greater than 6,000 stars on GitHub and is in energetic use at Maybe probably the most fascinating factor about Mitosis is that it describes a reactive consumer interface as JSON. It represents declaratively in knowledge the complicated performance on the coronary heart of front-end frameworks, which supplies a basis for creating a common mannequin of front-end growth frameworks. Mitosis’s cross-framework method to JavaScript compilation factors to the potential of meta frameworks and platforms that builders may use to compose purposes at the next degree of abstraction.

Copyright © 2022 IDG Communications, Inc.