Split your UI into .mx files and reuse them like React components. Each
instance gets its own local state — two <Counter /> on one page never share
a count.
A component is any top-level function that returns JSX. Declaration style and export style compose freely — what matters is which binding the export names, not how it was written:
// Named exports — any of these spellings:
export function Card() { return <div>hi</div>; }
const Badge = () => <div>hi</div>; export { Badge }
const Chip = () => <div>hi</div>; export { Chip as Tag } // aliased
// Default exports — any of these spellings:
export default function App() { return <body>hi</body>; }
export default function() { return <body>hi</body>; }
export default (() => <body>hi</body>)
const Shell = () => { return <body>hi</body>; }; export default Shell
const Panel = () => { return <body>hi</body>; }; export { Panel as default }
// Function expressions are the same component as arrows:
const Dash = function () { return <body>hi</body>; }; export default DashImports follow the same names: import App from './App.mx',
import { Card } from './ui.mx', or import { Card as C } from './ui.mx'.
import Hero from './components/Hero.mx';
import { Card } from './components/ui.mx';
export default function App() {
return (
<body>
<Hero title="hi" />
<Card t="x" />
</body>
);
}Rules:
import Hero from ...) binds the file's default export.import { Card } from ...) bind exported components by name.import Hero, { Card } from './ui.mx'.export { default as Card } from './Card.mx',
export { Card } from './Card.mx', and export * from './Card.mx' all
make import { Card } from './components' resolve to the component that
declares it.morph check flags
unknown components (mx-component-unknown) before you compile.Declare props with a typed parameter:
export function Hero(props: { title: string }) {
return <div>{props.title}</div>;
}You can also destructure. At each call site:
mx-component-required).mx-component-prop).key is reserved for list rendering and is never passed as a prop.Props typed as functions accept a named function or an inline arrow:
export function Button(props: { label: string, onPress: () => void }) {
return <button onClick={props.onPress}>{props.label}</button>;
}
export default function App() {
return (
<body>
<Button label="Save" onPress={() => console.log("saved")} />
</body>
);
}Inline arrows on function-typed props are compiled into typed adapters, so they behave like normal callbacks.
morphState inside a component belongs to that instance. Rendering <Hero />
twice gives two independent states, even though both run the same code:
import { morphState } from 'morph';
export function Hero(props: { title: string }) {
const [count, setCount] = morphState(0);
return <div>{props.title}: {count}</div>;
}Limitation: components rendered inside a .map() list template currently
share one state slot per template (per-instance state in lists is planned).
Refer to the `morphShared` API documentation for details on creating module-scoped shared state.
Refer to the `morphEvent` API documentation for details on creating typed event channels between components.
Use standard CSS imports. CSS.load() is deprecated:
import "./hero.css";morph check warns with mx-css-load-deprecated on CSS.load() calls.
| Code | Meaning |
|---|---|
mx-component-unknown |
Component tag is neither defined nor imported |
mx-component-prop |
Unknown prop passed to a component |
mx-component-required |
Required prop missing at a call site |
mx-state-scope |
morphState is called inside a component body |
mx-shared-scope |
morphShared is exported at module scope |
mx-event-scope |
morphEvent is exported at module scope |
mx-api-removed |
Removed string-key/shared/event APIs are not used |
mx-effect-nested |
morphEffect must be a top-level component-body statement |
mx-css-load-deprecated |
CSS.load() — use import "./x.css" |