Intro to Mitosis: The common reactive transformer

Mitosis.js is a compiler device that consumes a common element syntax and outputs framework-specific code. Which means you possibly can write utility 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 must summary front-end frameworks into pluggable elements. 

The hidden good thing about Mitosis is its revelation of the frequent facets and unity in front-end JavaScript frameworks. It is a new mannequin that might yield sudden insights and new instructions for future JavaScript improvement.

What’s Mitosis.js?

Mitosis is a venture from the parents at, who additionally developed envelope-stretching tasks just like the Qwik.js framework and Partytown. Builder is itself a superb instance of the sort of utility that advantages from Mitosis. Briefly, Builder means that you can visually design UI layouts throughout numerous underlying framework implementations. It wants a typical language to course of and output these various 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 device Figma. That’s to say, the abstraction layer is beneficial 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 exhibits off a couple of conventions of Mitosis’s JSX variant.

Itemizing 1. Primary listing output in Mitosis (TypeScript)

import  useStore  from '';

kind 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>(track, index) => <div>track.title</div></For>

Itemizing 1 takes an inventory of objects (songs) and outputs a property from each (track.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 useful element. Mitosis will rework this element to the best construction for its goal framework. 

Subsequent, word that the element 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> element. Iterating over collections is a type of areas of range in frameworks and the <For> element affords a easy, unified solution to specific it.

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

Run the compiler

To place this element by means of the Mitosis compiler (or, strictly talking, transpiler), we are able to arrange a easy Node Bundle Supervisor (NPM) venture. To start out, provoke a venture (npm init), then set up the Mitosis libraries by coming into

npm set up

The Mitosis compiler will robotically discover the information we wish 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 element 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) easy methods 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. It will drop information into the output/ listing, one department for every goal framework. Let’s take a peek on the /output/react/src/elements/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, track.title)));

  SongList as default

So, we are able to 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 element as a default module. This seems like legitimate React to this point, however let’s test it. 

We are able to 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/elements/Songs.jsx file from our Mitosis venture. We open App.jsx and import the brand new element by including import “./Songs.jsx” as Songs, then go into the template markup and use the element someplace with <Songs />

Now, we are able to run the app with npm begin. Examine the output at localhost:3000 and also you’ll see the listing of track names on the web page.

Good. Now we all know that Mitosis is working with React. In a real-world state of affairs, 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/elements/Songs.svelte (noticing that Mitosis has given the right 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 track listing on the best aspect of the display. 

Mitosis is producing appropriate Svelte. In the event you’re curious, Itemizing 5 exhibits the idiomatic Svelte iteration for the <For> element.

Itemizing 5. Track iterator in Svelte

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

You’ll be able to take comparable steps to confirm the correctness of every of the opposite output targets. 

Configuration and plugins

Mitosis is meant to be fairly versatile. Specifically, the Mitosis playground demonstrates the flexibility to vary a configuration to pick out not solely completely different frameworks however completely different traits inside them. As an illustration, you possibly can decide a state supplier in React, selecting between useState, Mobx, and Stable. You can also choose completely different styling options, like Emotion CSS, Styled Elements, 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 information construction is generated.

Consuming framework code

You would possibly surprise whether it is doable 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 might not solely allow us to two-directionally translate between Mitosis and a framework however really translate between completely 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 stated:

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


Mitosis is at present nonetheless in beta. That being stated, it has greater than 6,000 stars on GitHub and is in energetic use at Maybe probably the most attention-grabbing factor about Mitosis is that it describes a reactive person interface as JSON. It represents declaratively in information the advanced performance on the coronary heart of front-end frameworks, which supplies a basis for growing a common mannequin of front-end improvement 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 stage of abstraction.

Copyright © 2022 IDG Communications, Inc.