Component per file pattern
See original GitHub issueI 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:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
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.
For future readers: one thing this really helps solve is understanding that: