Made in Builder

×

Developers

Product

Use Cases

Pricing

Developers

Resources

Company

Get StartedLogin

Product

Features

Integrations

Talk to an Expert

Pricing

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

‹ Back to blog

A Quick Guide to Mitosis: Why You Need It and How You Can Use It

April 14, 2022

Written By Alex Patterson

I have worked for several companies, both as a full-time employee and as a contractor. One thing has always remained true: There are multiple languages and frameworks being used across their frontend applications. (Well, okay, there was that small startup that only had one, but this still applies.) Often several teams have people with different skill sets, and they might decide that it’s easier to go with React. Another team might go with Vue because they feel it’s the next great framework.

You might be thinking, "What’s the big deal? It’s all just JavaScript anyway."  Sure, you’re right, but the real challenge here is when each of these teams need to coexist within a single brand that has a very specific design system. This is where Builder.io's Mitosis can help you create a single source of truth for all of the reusable components within your Design System.

What is Mitosis?

Mitosis is a compile-time framework that allows you to write components in JSX and compile to vanilla JavaScript, Angular, React, Vue and more.

Mitosis uses a static subset of JSX, inspired by Solid. This means we can parse it to a simple JSON structure, then easily build serializers that target various frameworks and implementations.

This might sound very similar to the work the Ionic team did with Stencil. The one main difference is that we're not just outputting web components. Rather full framework-ready JavaScript. You can even compile the output to Stencil as well.

As of April 2022, Builder.io's Mitosis supports

You can try this yourself in Builder.io's Mitosis Fiddle pictured below.

How does this compare to other compile time frameworks?

Two very popular compile-time frameworks are Svelte and SolidJS. Mitosis is very similar to these in terms of it being a compile time framework which makes it very fast. Where it differs is that it lets you produce multiple frameworks, allowing for the most flexibility.

Similar to SolidJS, Mitosis uses a version of JSX that compiles the components to JSON. Plugins then compile the components to different targets, enabling you to create tooling in two directions:

  • Code that can be converted into Mitosis JSON
  • Plugins that compile or serialize JSON to target frameworks

Mitosis is supported by Builder.io, because Mitosis also supports no-code tools. It does this by following the above serialization to Builder.io's JSON format. This makes Mitosis a very stable product because we will need to continue updating and improving the code base so that Builder.io remains successful.

How to install the CLI

First install the CLI by opening your terminal and executing:

The mitosis CLI has simple commands to follow but if you ever forget, you can run mitosis --help. Which will show you the usage and examples.

It is important to remember that Mitosis is just the compiler here. We'll still need to create the JSX files that represent our component, which Mitosis will then output.

How to create a Mitosis Project

First create a new directory and change into it:

Now create an npm project so that we can use the @builder.io/mitosis package:

I would also highly recommend initializing this directory with git (this is optional):

If you're using git make sure to create a .gitignore file with the following:

Install the Builder.io Mitosis package into the local project:

Add JSX Mitosis File

The file name should end with lite.jsx this allows the VSCode extension to be used.

Open your project in VSCode:

Your project should look like this in VSCode:

Add JSX to the component.lite.jsx file:

If you've ever written in React, this should feel very familiar to you.

State Comparison

Before you output our component.lite.jsx to any available language, take a close look at how state is used. The useState hook will be the same as how you use it in React. Below you can see a few of the frameworks and how they use this definition to appropriately assign the state variable for each input.

As you can see the state variable name used within the JSX, is correctly compiled in the other frameworks below.

Angular:

Svelte:

Vue:

Mitosis Compilation

Now that you have the Mitosis CLI setup, your component.lite.jsx file created, and a better understanding of how state works, it's time to generate your own framework files.

I like to keep my output files in a separate folder. To create that folder execute:

To start compiling open a terminal in the same directory as your component.lite.jsx file (in VSCode Ctrl + `) and run the below command:

The --to= flag sets the framework that you compile your Mitosis component output to, followed by the location of your Mitosis input file component.lite.jsx, > represents where the output will be placed, and finally the output file location including any directory output/component.jsx.

After running the above example you will get the below React code.

React

Command:

output/component.jsx

Angular

Command:

output/component.tsx

Svelte

Command:

output/component.svelte

Vue

Command:

output/component.vue

Conclusion

Mitosis is the easiest way to future-proof your current development. If you're starting a new greenfield project, it will be even more important to future-proof, as well as far simpler to start writing from scratch rather than converting your current components.

Mitosis allows multiple teams to work on a single unified language no matter what framework they're using. This reduces the amount of duplicate components and lowers technical debt.

Have more questions? See the Mitosis Docs.

Full example can be found at https://github.com/codercatdev/mitosis-example

Share

Twitter
LinkedIn
Facebook

Continue Reading
-36 MIN
Our current frameworks are O(n); we need O(1)
WRITTEN BYMiško Hevery
May 17, 2022
4 MIN
Fast to Faster: What You Need to Do to Improve Performance in Next.js
WRITTEN BYSteve Sewell
May 13, 2022
9 MIN
Don't blame the developer for what the frameworks did!
WRITTEN BYMiško Hevery
May 4, 2022

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Visually build and optimize digital experiences on any tech stack. No coding required, and developer approved.

Sign up

Log in

DEVELOPERS

Builder for Developers

Developer Docs

Github

JSX Lite

Qwik

INTEGRATIONS

React

Angular

Next.js

Gatsby

PRODUCT

Product features

Pricing

RESOURCES

User Guides

Blog

Forum

Templates

COMPANY

About

Careers 🚀

Visually build and optimize digital experiences on any tech stack. No coding required, and developer approved.
Get Started
Log in

DEVELOPERS

Builder for Developers

Developer Docs

Open Source Projects

Performance Insights

INTEGRATIONS

All Integrations

Shopify

React

Angular

Next.js

Gatsby

RESOURCES

User Guides

Blog

Community Forum

Templates

Partners

Submit an Idea

PRODUCT

Features

Pricing

© 2022 Builder.io, Inc.

Security

Privacy Policy

Terms of Service