Skip to content

RFC 20: Render props: {view (row, i) { … }} #21

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: none beyond upstream PR solidjs#69.

Summary

A declared JS component whose children is typed as a function returning a View takes a render prop. In .rx it is written as a view lambda among the children, {view (row, i) { … }}, and a named one, {view header (n) { … }}, fills the prop of that name. The body is markup, and it is checked as the component's children are (MO-11k).

Motivation

Many Solid libraries call a function for each thing they render:

  • A virtual list (@tanstack/virtual-core wrapped as a component) calls its children for each visible row.
  • Formisch's <Field of={form} path={["email"]}>{(field) => <input {...field.props} />}</Field> passes the field's state.

On upstream, .rx has no way to write such a function. An arrow in reactive scope is a pure expression and cannot hold a view with its own declarations, and nothing checks a view built inside a JS call. So these components cannot be used from .rx, and wrapping them in JS loses every check on the row's body.

Proposal

MO-11k (Render props). A declared JS component whose children field is typed as a function giving a View (children: (item: T, index: number) => View) takes a render prop. It is written as the application's child, a view lambda:

// virtual.d.rx
export component VirtualList(props: { items: any[]; height?: number; children: (item: any, index: number) => View; header?: (count: number) => View })
export default component App() {
  source picked := -1
  provide theme = "dark"
  errored fallback(e) { <p>failed</p> } {
    loading fallback { <p>loading…</p> } {
      <VirtualList items={["a", "b", "c"]} height={300}>
        {view header (n) { <p>{n} rows, {use theme}</p> }}
        {view (row, i) {
          detail = await fetch(`detail:${row}`)
          <button class={use theme} onClick={() => { picked := i }}>{row} {detail}</button>
        }}
      </VirtualList>
    }
  }
}
  • Parameters are values, what the library passes on each call, like a for row's binding.
  • The body is a reactive block, declarations and then a view, owned where the component calls it. Handlers in it write sources as any handler does.
  • Checks treat the body as the application's children (RG-4c, MO-11b). Its failures and the contexts it uses are checked above the application, through the component's declared catches(…) and provides(…). Its content model is checked where the component places its children (SV-7d).
  • Lowering. It compiles to children: (row, i) => … and projects to the same arrow, so TypeScript types row and i from the library's own types.
  • Named render props. {view header (n) { … }} among the children is the header prop (header={(n) => …}), checked the same way.
  • Errors. Other children of a component that takes a render prop are VIEW_FN_EXPECTED. A view lambda anywhere else, including under a name the declaration does not type as a function giving a View, is VIEW_FN_MISPLACED.

Drawbacks and alternatives

  • view becomes a keyword in this position. The alternative, allowing a statement-bodied arrow that returns markup, would mix reactive and imperative scope (SC-3) and cannot be checked as children.
  • Named render props are written among the children, not as attributes. That keeps markup out of attribute expressions, but it is a second place a prop can come from.
  • Alternative: allow only children. The virtual list's header and many kits' fallback-style props need names.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript:

  • ec7cd13: render props as children, in both parsers, checkers, emitters and projections, with rx-declare drafting such children.
  • 656d035, the MO-11k part: named render props.

Spec: MO-11k. Scenario: S-module-14 (accepted; UNHANDLED_FAILURE, CONTEXT_NOT_FOUND, VIEW_FN_EXPECTED, VIEW_FN_MISPLACED for a View child and for an undeclared name, and REACTIVE_WRITE_IN_OWNED_SCOPE in the body). examples/virtual/main.rx runs a VirtualList with a header render prop in Chromium (Vite suite). examples/formisch uses <Field>'s render prop (compiler/test/formisch.mjs). Both implementations (ref/ and the Rust crate) agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions