Skip to content

JSX expressions run in different contexts depending on how the JSX is used #353

Closed

Description

@GabbeV

Octane appears to have two different evaluation modes for JSX, with observable semantic differences.

When JSX is the rendered output of an @{ ... } block, expressions in its children are evaluated within the render context established by the containing component, provider, or boundary.

When JSX is used as a value, its child expressions are evaluated while constructing that value, in the surrounding owner’s context, before the represented component, provider, or boundary has been entered.

For example:

const Context = createContext("outer");

function DirectRender() @{
	<Context.Provider value="inner">
		<span>{use(Context)}</span>
	</Context.Provider>
}

function ValueRender() @{
	const content = (
		<Context.Provider value="inner">
			<span>{use(Context)}</span>
		</Context.Provider>
	);

	<div>{content}</div>
}

DirectRender renders "inner" because the context read is evaluated within the provider’s context.

ValueRender renders "outer" because the context read is evaluated while constructing content, in the context surrounding the provider value.

The same distinction affects context reads, suspension ownership, and error ownership. Moving otherwise identical JSX into a variable, prop, or another value position can therefore change which provider or boundary applies to expressions inside it.

Control-flow directives switch back to scoped evaluation

A control-flow directive such as @if, @for, @switch, or @try inside value JSX creates its own compiled render context.

For example:

<Foo
	slot={
		<div>
			@try {
				<span>{boom()}</span>
			} @catch {
				<span>caught</span>
			}
		</div>
	}
/>

The slot prop is still a value, but boom() is evaluated within the context established by the generated @try block.

A value can therefore contain nested regions that follow the scoped evaluation semantics of an @{ ... } block, even though the surrounding JSX follows value semantics.

ErrorBoundary is special-cased

PR #158 introduced special lowering for statically recognized <ErrorBoundary> elements.

As a result, this:

<ErrorBoundary fallback="inner">
	<span>{boom()}</span>
</ErrorBoundary>

behaves like an @try block. Its children are evaluated within the context of the inner boundary, even when the JSX otherwise appears in a value position.

A wrapper remains in value mode:

function MyErrorBoundary(props) {
	return (
		<ErrorBoundary fallback={props.fallback}>
			{props.children}
		</ErrorBoundary>
	);
}

<MyErrorBoundary fallback="inner">
	<span>{boom()}</span>
</MyErrorBoundary>

Here, boom() is evaluated while constructing the MyErrorBoundary value, in the surrounding context. The wrapper and its inner boundary have not yet been entered, so an outer boundary catches the error instead.

The built-in and wrapped forms therefore have different semantics despite representing the same apparent component structure:

<ErrorBoundary fallback="inner">
	<span>{boom()}</span>
</ErrorBoundary>

<MyErrorBoundary fallback="inner">
	<span>{boom()}</span>
</MyErrorBoundary>

Problem

The effective behavior is:

Form Evaluation context
JSX rendered directly by an @{ ... } block The context established by the containing component, provider, or boundary
JSX used as a value The surrounding owner’s context while constructing the value
Control-flow directive inside value JSX The context established by the directive
Recognized built-in ErrorBoundary The context of the generated @try-like boundary
Wrapper around ErrorBoundary The surrounding value-construction context

These evaluation-mode changes are not apparent from the authored component tree and can change context, Suspense, and error-boundary behavior.

The special handling of ErrorBoundary makes this particularly surprising, because replacing it with an apparently equivalent wrapper changes which boundary catches an error.

Why this matters

React’s caller-owned evaluation is often undesirable for composition. Octane’s @{ ... } semantics could instead be a major selling point if applied consistently.

Rather than documenting two evaluation modes, JSX values could behave as though immediately placed in an @{ ... } block, removing the distinction entirely. Context, suspension, and error ownership would then follow the structure visible in the component tree.

The current value semantics are also a significant deviation from React that is missing from the documentation.

Activity

  1. GabbeV commented on Jul 28, 2026

    @GabbeV
    Author

    @trueadm I’m curious why this selective thunking approach is preferable to making value-position JSX use the same scoped render-body model as @{}.

    The heuristic seems hard to make robust because many expressions can execute user code without containing an explicit call:

    Property access: obj.x, obj[key], obj?.x
    Assignment/update: obj.x = value, obj.x += value, value++, destructuring
    Coercion: +value, a + b, a < b, a == b, in, instanceof, ${value}
    Iteration/reflection: [...value], { ...value }
    Computed keys and class evaluation
    Identifier reads that hit TDZ or unresolved bindings

    These can invoke getters, Proxy traps, setters, coercion hooks, iterators, or static initializers.

    I also noticed that the simultaneous docs PR #354 explicitly avoids making claims about #353. Is there some reason we don't want to promise these semantics or is explaining that just left for the future?

    Could value-position JSX instead allocate one render body at its root and compile like a normal component body? That might allow the descriptor/de-opt rendering path to be removed entirely simplifying the library. I may be missing a compatibility or performance constraint, so I’d be interested in the reasoning.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions