question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component per file pattern

See original GitHub issue

I was wondering, what’s the reason for choosing the “react-style” source code layout of components - as opposed to a more Svelte-like code layout, where every file is implicitly a component that can be instantiated?

To illustrate what I mean - taking this example:

import { useState } from "@builder.io/mitosis";

export default function MyComponent(props) {
  const [name, setName] = useState("Steve");

  return (
    <div>
      <input
        css={{
          color: "red",
        }}
        value={name}
        onChange={(event) => setName(event.target.value)}
      />
      Hello! I can run in React, Vue, Solid, or Liquid!
    </div>
  );
}

You could instead have something like:

let name = "Steve";

function setName(event) {
  name = event.target.value;
}

export default (
  <div>
    <input
      css={{
        color: "red",
      }}
      value={name}
      onChange={setName}
    />
    Hello! I can run in React, Vue, Solid, or Liquid!
  </div>
);

Obviously this isn’t meaningful Typescript on it’s own - this pattern assumes a component-per-file pattern (which most people will likely prefer anyway) and the compiler would take care of all the instance-related stuff, binding variables to reactive state containers, etc.

Since your intermediary format is just JSON, implementing something like this wouldn’t require much more than the TS parser API and an analysis layer that identifies reactive variables and so on.

Thoughts? 🙂

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
PatrickJScommented, Mar 24, 2022

I think its better to lower the scope of mitosis. So going the babel route.

Mitosis provides a as much info on a mitosis file (the json file) and the dev can use that to generate the correct file(s). Mitosis can provide their own versions of each one framework as examples. And mitosis can talk about tradeoffs etc. This will allow the dev to have full control over the output conventions to fit their use. Otherwise mitosis would have to support unlimited amount of output conventions.

1reaction
steve8708commented, Mar 3, 2022

For future readers: one thing this really helps solve is understanding that:

  1. Mitosis is not react
  2. Mitosis is static, declarative, and each file can only have one component. There are currently a lot of gotchas if you treat mitosis like react and try to add all kind of random variable declarations, multiple components in a file, etc which this helps make the rules more intuitive in clear
Read more comments on GitHub >

github_iconTop Results From Across the Web

дэн on Twitter: "I still get surprised that “one function per file” is ...
Just wrote a 300 line file with multiple components and it feels really good to not split them into different files. Show this...
Read more >
Multiple components within a file - Stack Overflow
We can create as many components as we want in the same file, and we can use those components in the same way...
Read more >
Airbnb React/JSX Style Guide - javascript - GitHub
Basic Rules. Only include one React component per file. However, multiple Stateless, or Pure, Components are allowed per file. eslint: react/no-multi-comp .
Read more >
Writing Scalable React Apps with the Component Folder Pattern
Discover how to organize your React components using the component folder pattern. It will help un-clutter your projects, and your life.
Read more >
Is it good practice to put multiple components in one file like ...
This is a component in an app I am working on. The file exports one function "ProfileInfoCard". Within this file there are two...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found