Severity: error | Blocks morph build: yes
A helper hoisted to module scope captures route state or props, which only exist on the mount context at runtime:
error : mx-route-state : route /add references route state/props from module scope (morph::Result<JsValue> inst2_checkIp()); move it into the component body or pass explicit parameters
Learn more: https://morph.levizr.com/docs/errors/mx-route-stateRoute state (morphState) lives per mount on a generated context struct, not
in globals — two windows on one route stay independent. A namespace-scope
helper has no context pointer, so plain helpers that capture setters/getters
are rewritten into context-taking templates (template <typename __MorphCtx>
with a std::shared_ptr context parameter, threaded through at every call
site). That covers ordinary functions. What still cannot compile — and keeps
this error — is mount state referenced from places that take no parameters:
The entry build has no mount context, which is why the same component can compile fine as the app entry but fail as a route.
// ❌ Class members, module-scope lambdas/consts, and keyed-list item
// templates cannot take a context parameter — mx-route-state.
// (Plain helpers like the one below compile via context templates.)
export default function IpBadge() {
const [ip, setIp] = morphState("")
async function checkIp() {
const r = await fetch("http://api.ipify.org")
if (r.ok()) { setIp(r.text()) }
}
morphEffect(() => { checkIp() }, [])
return (<div>{ip}</div>)
}Ordinary stateful helpers (like checkIp above) compile as-is — the build
threads the mount context through automatically. If you still see this
error, the state access sits somewhere parameterless:
// ✅ no module-scope helper; everything runs with the mount context
morphEffect(() => {
fetch("http://api.ipify.org").then((r) => { if (r.ok()) { setIp(r.text()) } })
}, [])morphShared stores, which stay global across mounts).src/App.mx) and use stateless components plus morphShared
stores inside routes.Do not disable this rule. A context-free helper cannot reach mount state.
morphState outside a component