LuxelEspañol

Luxel v0.2 · Reference · part 2 of 3

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

#Habit from ReactCodeRule
20useMemo for a derived totalLUXEL-E003no-hooks
21read a preference from localStorageLUXEL-E053browser-api-in-view
22onSubmit on the formLUXEL-E001no-function-handler
23an input's value set from a variableLUXEL-E010unknown-prop
24a ref to focus an inputLUXEL-E010unknown-prop
25an environment variable read in the componentLUXEL-E054env-in-view
26read the URL from window.locationLUXEL-E053browser-api-in-view

1. A counter in component state

import { Action, Text, View, useState } from '@luxel-framework/ui'; export default function Cart() { const [count, setCount] = useState(0); return (‹View title="Cart" dataClass="internal">‹Text id="n">{String(count)}‹/Text>‹Action id="add" event="cart.add">Add‹/Action>‹/View>); }

src/views/r01-use-state.view.tsx:4:30 — LUXEL-E003 (no-hooks)

  • Why: "useState" is a hook, and @luxel-framework/ui has no hooks: a view has no client state or effects.
  • Instead: Bind server state with ‹Text bind="...">, send changes with ‹Action event="..."> (defineEvent on the server), and declare local transitions in onSuccess.

2. UseEffect to load data after mount

import { useEffect } from 'react'; import { Text, View } from '@luxel-framework/ui'; export default function Cart() { useEffect(() => {}, []); return (‹View title="Cart" dataClass="internal">‹Text bind="cart.summary" />‹/View>); }

src/views/r02-use-effect-fetch.view.tsx:4:27 — LUXEL-E003 (no-hooks)

  • Why: "useEffect" 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: Data is read on the server before the page leaves: defineLoad in server/events.ts, shown with bind="…". Data that changes on the server later: ‹Action poll> (polling); after an action: the handler's answer.

3. OnClick that updates state

import { Action, View } from '@luxel-framework/ui'; export default function Cart() { let count = 0; return (‹View title="Cart" dataClass="internal"> ‹Action id="add" event="cart.add" onClick={() => { count += 1; }}>Add‹/Action> ‹/View>); }

src/views/r03-onclick-logic.view.tsx:8:7 — LUXEL-E001 (no-function-handler)

  • Why: "onClick" on ‹Action> 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).

4. A controlled input with onChange

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

src/views/r04-controlled-input.view.tsx:7:7 — LUXEL-E001 (no-function-handler)

  • Why: "onChange" on ‹Field> 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).

5. A line total computed in JSX (qty × price)

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

src/views/r05-math-on-item.view.tsx:6:5 — LUXEL-E016 (item-field-misuse)

  • Why: The item template computes with "item.qty" 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.qty}). 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.qty} with stateClasses.

6. Fetch the data inside the component

import { Text, View } from '@luxel-framework/ui'; export default function Cart() { void fetch('/api/cart'); return (‹View title="Cart" dataClass="internal">‹Text bind="cart.summary" />‹/View>); }

src/views/r06-fetch-in-view.view.tsx:6:8 — LUXEL-E052 (data-fetching-in-view)

  • Why: "fetch" in a view. A view runs once, at build time, in Node: there is no request, no user and no browser then, so data cannot be fetched there. Data is server state.
  • Instead: Declare the data in server/contract.ts, read it in defineLoad (server/events.ts) and show it with bind="…". Anything that changes after the page loads is an ‹Action event> handled by defineEvent.

7. Map with logic (render only the active items)

import { Collection, Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Collection bind="cart.items" itemKey="id" maxItems={20}> {(i) => (String(i.active) === 'on' ? ‹Text>{i.name}‹/Text> : null)} ‹/Collection> ‹/View>);

src/views/r07-conditional-on-item.view.tsx:6:5 — LUXEL-E016 (item-field-misuse)

  • Why: The item template computes with "item.active" 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.active}). 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.active} with stateClasses.

8. An inline style object

import { Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Text id="title" as="h1" style={{ color: 'red' }}>Cart‹/Text> ‹/View>);

src/views/r08-inline-style.view.tsx:6:5 — LUXEL-E010 (unknown-prop)

  • Why: ‹Text> has no prop "style": 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: Inline styles are not part of the manifest grammar: use className and your stylesheet.

9. A link whose href is built from the item

import { Collection, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Collection bind="cart.items" itemKey="id" maxItems={20}> {(i) => ‹a href={`/items/${i.id}`}>{i.name}‹/a>} ‹/Collection> ‹/View>);

src/views/r09-dynamic-href.view.tsx:6:5 — LUXEL-E016 (item-field-misuse)

  • Why: The item template computes with "item.id" 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.id}). 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.id} with stateClasses.

10. A className chosen from the item's data

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 className={i.active}>{i.name}‹/Text>} ‹/Collection> ‹/View>);

src/views/r10-class-from-item.view.tsx:7:15 — LUXEL-E028 (dynamic-attribute)

  • Why: "className" on ‹Text> takes an item field: a dynamic attribute outside the closed list.
  • Instead: Only text ({item.field}), param={item.key} and, on ‹Text>, state={item.field} with stateClasses come from an item; the rest of an item template is static.

11. Import React

import React from 'react'; import { Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal">‹Text id="t">{String(React)}‹/Text>‹/View>);

src/views/r11-import-react.view.tsx:4:19 — LUXEL-E002 (no-react-import)

  • Why: Import of "react". React (and any client component library) does not exist in a Luxel page: the browser receives a compiled template and the universal runtime, never app code.
  • Instead: Import components from "@luxel-framework/ui" (View, Stack, Row, Text, Field, Action, Collection, Status). Your own helper components and data files are fine.

12. A plain ‹button> element

import { View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹button type="button">Add‹/button> ‹/View>);

src/views/r12-html-button.view.tsx:6:5 — LUXEL-E004 (unknown-component)

  • Why: ‹button> is not a Luxel component: there are no HTML elements in a view.
  • Instead: Use ‹Action event="..."> for a button that sends a server event.

13. Key on the mapped child instead of itemKey

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

src/views/r13-key-instead-of-itemkey.view.tsx:6:5 — LUXEL-E008 (collection-without-key)

  • Why: ‹Collection> has no key: each item needs a stable identity (like React's key).
  • Instead: Write itemKey="id" (the key the server contract declares).

14. DangerouslySetInnerHTML for formatted text

import { Text, View } from '@luxel-framework/ui'; export default () => (‹View title="Cart" dataClass="internal"> ‹Text id="note" dangerouslySetInnerHTML={{ __html: '‹b>Hi‹/b>' }} /> ‹/View>);

src/views/r14-dangerously-set-html.view.tsx:6:5 — LUXEL-E010 (unknown-prop)

  • Why: ‹Text> has no prop "dangerouslySetInnerHTML": 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: HTML never travels in VSA: everything is written as text. Use ‹Text>.
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.