LuxelEspañol

Luxel v0.2 · Reference · part 3 of 3

Coming from React: the typical mistakes and what Luxel says (3)

15. Read the session in the component (next-auth)

import { useSession } from 'next-auth/react'; import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const session = useSession(); return (‹View title="Cart" dataClass="internal">‹Text id="who">{String(session)}‹/Text>‹/View>); }

src/views/r15-use-session.view.tsx:4:28 — LUXEL-E003 (no-hooks)

  • Why: "useSession" is a React hook. Luxel views run once, at build time, and nothing of them runs in the browser: there is no client state or effect to hook into.
  • Instead: Who may see and do what is declared as policies in server/contract.ts (allow(["role"]), publicEvent()) and checked by the gateway; the session is ctx.session in handlers, never in the view.

16. Navigate with useRouter (Next.js)

import { useRouter } from 'next/navigation'; import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const router = useRouter(); return (‹View title="Cart" dataClass="internal">‹Text id="t">{String(router)}‹/Text>‹/View>); }

src/views/r16-use-router.view.tsx:4:27 — LUXEL-E003 (no-hooks)

  • Why: "useRouter" is a React hook. Luxel views run once, at build time, and nothing of them runs in the browser: there is no client state or effect to hook into.
  • Instead: Navigation is the server's: return ctx.navigate("‹view>") from the handler of an ‹Action event="…">; a record's id travels as param={item.id}.

17. An async server component that awaits its data

import { Text, View } from '@luxel-framework/ui'; export default async function Cart() { const data = await Promise.resolve({ summary: '3 items' }); return (‹View title="Cart" dataClass="internal">‹Text id="s">{data.summary}‹/Text>‹/View>); }

src/views/r17-async-server-component.view.tsx:5:1 — LUXEL-E025 (async-component)

  • Why: The view is async (its default export returned a Promise).
  • Instead: Views are synchronous: the server reads the data in defineLoad (server/events.ts), and the view shows it with bind="…".

18. Today's date computed in the component

import { Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Text id="today">{new Date().toISOString()}‹/Text> ‹/View>);

src/views/r18-today-in-view.view.tsx:6:23 — LUXEL-E026 (non-deterministic-view)

  • Why: "new Date()" in a view. A view is compiled once into a public template that must be the same bytes on every build; time and randomness make it differ.
  • Instead: Remove it from the view. A date or time the page shows is the server's: send it as a date() field from defineLoad. An id is the server's too.

19. Money formatted in JSX (cents / 100, toFixed)

import { Collection, Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Collection bind="cart.items" itemKey="id" maxItems={20}> {(i) => ‹Text>{(Number(i.price) / 100).toFixed(2)}‹/Text>} ‹/Collection> ‹/View>);

src/views/r19-format-money-in-view.view.tsx:6:5 — LUXEL-E016 (item-field-misuse)

  • Why: The item template computes with "item.price" at build time. Items arrive later, in the server state: their fields are placeholders, so arithmetic, conditions, formatting and URLs built from them cannot work here.
  • Instead: Place the field as it is ({item.price}). Compute on the server, in defineLoad/defineEvent: a derived value (a line total, a label) is another item field; filtering means the server sends only the items to show; money and dates are money()/date() fields formatted by the server; a link is ‹Action event="…" param={item.id}> with ctx.navigate() in its handler; a style that depends on the item is state={item.price} with stateClasses.

20. UseMemo for a derived total

import { useMemo } from 'react'; import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const total = useMemo(() => 1 + 1, []); return (‹View title="Cart" dataClass="internal">‹Text id="t">{String(total)}‹/Text>‹/View>); }

src/views/r20-use-memo.view.tsx:4:25 — LUXEL-E003 (no-hooks)

  • Why: "useMemo" is a React hook. Luxel views run once, at build time, and nothing of them runs in the browser: there is no client state or effect to hook into.
  • Instead: Derived values (totals, filters, labels) are computed on the server, in defineLoad or defineEvent, and shown with bind="…": the browser computes nothing.

21. Read a preference from localStorage

import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const theme = String(localStorage.getItem('theme')); return (‹View title="Cart" dataClass="internal">‹Text id="t">{theme}‹/Text>‹/View>); }

src/views/r21-local-storage.view.tsx:6:24 — LUXEL-E053 (browser-api-in-view)

  • Why: "localStorage" in a view. The view runs once, at build time, in Node, and none of its code reaches the browser, so browser APIs do not exist for it.
  • Instead: What the page shows comes from the server (defineLoad); preferences and per-user data live on the server, never in browser storage; navigation is ctx.navigate() in a handler; presentation (hover, theme, layout) belongs in your stylesheet.

22. OnSubmit on the form

import { Action, Field, Stack, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Stack as="form" onSubmit={() => undefined}> ‹Field name="name" label="Name" /> ‹Action id="add" event="cart.add">Add‹/Action> ‹/Stack> ‹/View>);

src/views/r22-on-submit.view.tsx:6:5 — LUXEL-E001 (no-function-handler)

  • Why: "onSubmit" on ‹Stack> is a function. Functions never run in the browser in Luxel: there are no event handlers.
  • Instead: Logic runs on the server: use ‹Action event="..."> and handle the event with defineEvent. Local presentation changes go in onSuccess (clear, focus).

23. An input's value set from a variable

import { Action, Field, Stack, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal">‹Stack as="form"> ‹Field name="name" label="Name" value="Ada" /> ‹Action id="add" event="cart.add">Add‹/Action> ‹/Stack>‹/View>);

src/views/r23-value-prop.view.tsx:6:7 — LUXEL-E010 (unknown-prop)

  • Why: ‹Field> has no prop "value": components take a closed set of props, because the compiled template and the manifest are a closed grammar (no HTML, no inline style, no handler).
  • Instead: There are no controlled inputs. A field starts with the value the server sends for it in the view's state (defineLoad), and what the person types is sent with the ‹Action> of its form.

24. A ref to focus an input

import { Action, Field, Stack, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal">‹Stack as="form"> ‹Field name="name" label="Name" ref={null} /> ‹Action id="add" event="cart.add">Add‹/Action> ‹/Stack>‹/View>);

src/views/r24-ref-focus.view.tsx:6:7 — LUXEL-E010 (unknown-prop)

  • Why: ‹Field> has no prop "ref": components take a closed set of props, because the compiled template and the manifest are a closed grammar (no HTML, no inline style, no handler).
  • Instead: There are no refs: nothing of the view runs in the browser. To move the focus after an action use onSuccess={{ focus: "‹zone id>" }}; to empty inputs, onSuccess={{ clear: ["‹field>"] }}.

25. An environment variable read in the component

import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const api = String(process.env.API_URL ?? ''); return (‹View title="Cart" dataClass="internal">‹Text id="api">{api}‹/Text>‹/View>); }

src/views/r25-env-in-view.view.tsx:6:22 — LUXEL-E054 (env-in-view)

  • Why: "process.env.API_URL" in a view. Its value would be written into a PUBLIC compiled template at build time, for anyone to read.
  • Instead: Read configuration and secrets on the server, in handlers or bindings, from the environment; a view only shows server state (bind="…").

26. Read the URL from window.location

import { Text, View } from '@luxel-framework/ui'; export default function Cart() { const here = String(window.location.pathname); return (‹View title="Cart" dataClass="internal">‹Text id="t">{here}‹/Text>‹/View>); }

src/views/r26-window-location.view.tsx:6:23 — LUXEL-E053 (browser-api-in-view)

  • Why: "window" in a view. The view runs once, at build time, in Node, and none of its code reaches the browser, so browser APIs do not exist for it.
  • Instead: What the page shows comes from the server (defineLoad); preferences and per-user data live on the server, never in browser storage; navigation is ctx.navigate() in a handler; presentation (hover, theme, layout) belongs in your stylesheet.
Edit this page on GitHub

This page: 7.9 KB of JavaScript (Luxel's shared runtime), 0 KB from your app.

Measured when the site is built: the runtime files this page loads, compressed as they are sent.

Made by IntiOps. The Luxel 0.1.0 betas are on npm (0.1.0-beta.1, 0.1.0-beta.2), with an alpha of 0.2 (0.2.0-alpha.1): no external security review yet.