Skip to content

Page and layout props ​

The current Inertia page (page, usePage), and the functions a page uses to hand props up to its layouts (setLayoutProps, resetLayoutProps).

page ​

The current page: its component name, props, URL, flash data and the rest of what the server sent. Every read is tracked, so templates and getters that use it update when Inertia swaps the page or a partial reload changes its props. It's read-only.

gts
import { page } from 'inertia-ember';

<template>
  {{#if page.props.auth.user}}
    Signed in as
    {{page.props.auth.user.name}}
  {{/if}}
</template>

Signature ​

ts
const page: Page<SharedPageProps>;

Fields ​

The ones you'll read most; the full shape is Page from @inertiajs/core.

NameTypeDescription
componentstringThe page's name, as the server sent it (Users/Show).
propsPageProps & SharedPageProps & { errors }The page's props, the shared ones included. errors holds the validation errors.
urlstringThe page's URL, path and query.
versionstring | nullThe asset version the server sent.
flashFlashDataFlash data for this visit, typed by flashDataType.
deferredPropsRecord<string, string[]> | undefinedThe props the server will send later, by group.
clearHistory, encryptHistoryboolean | undefinedThe history flags the server set.

Setting a field throws: [inertia-ember] The page is read-only. Use router.replace() to change it.

Examples ​

Read shared props anywhere ​

page works outside pages too: in a layout, a component deep in the tree, or a getter.

gts
import Component from '@glimmer/component';
import { page } from 'inertia-ember';

export default class UserMenu extends Component {
  get initials() {
    return page.props.auth.user?.name.slice(0, 2).toUpperCase();
  }

  <template>
    <span class='avatar'>{{this.initials}}</span>
  </template>
}
gts
import { Link, page } from 'inertia-ember';

const isCurrent = (prefix: string) => page.url.startsWith(prefix);

<template>
  <Link @href='/orders' class={{if (isCurrent '/orders') 'active'}}>Orders</Link>
</template>

Show flash messages ​

The server flashes a toast; with flashDataType declared it's typed:

gts
import { page } from 'inertia-ember';

<template>
  {{#if page.flash.toast}}
    <div role='status'>{{page.flash.toast}}</div>
  {{/if}}
</template>

usePage ​

The same page as page, with its props typed as your shared props plus the ones you name. It takes no owner and needs no cleanup.

ts
import { usePage } from 'inertia-ember';

const { props } = usePage<{ invoice: { number: string } }>();

props.invoice.number; // string
props.auth.user; // from sharedPageProps

Signature ​

ts
function usePage<TPageProps extends object = PageProps>(): Omit<Page<SharedPageProps>, 'props'> & {
  props: PagePropsOf<TPageProps>;
};

Returns ​

The page object, typed. props is PagePropsOf<TPageProps>: your props, the shared ones and errors, with no catch-all key, so a prop you didn't name is a type error.

Examples ​

Typed props in a component that isn't a page ​

gts
import Component from '@glimmer/component';
import { usePage } from 'inertia-ember';

export default class InvoiceTotal extends Component {
  get total() {
    const { lines } = usePage<{ lines: { amount: number }[] }>().props;

    return lines.reduce((sum, line) => sum + line.amount, 0);
  }

  <template>{{this.total}}</template>
}

Call it where you read, as here in a getter, or keep the result: it's the same tracked object either way.

setLayoutProps ​

Passes props from a page up to its layouts. Layouts get them in @props, merged over the page's props. With an object they reach every layout; with a name first, only the layout of that name.

ts
import { setLayoutProps } from 'inertia-ember';

setLayoutProps({ title: 'Inbox' });
setLayoutProps('sidebar', { collapsed: true });

Signature ​

ts
function setLayoutProps(props: Partial<LayoutProps>): void;
function setLayoutProps<K extends keyof NamedLayoutProps>(name: K, props: Partial<NamedLayoutProps[K]>): void;

Arguments ​

NameTypeDescription
namekeyof NamedLayoutPropsOptional. The name of the layout to reach, from a page's named layouts ({ sidebar: SidebarLayout }).
propsPartial<LayoutProps> or Partial<NamedLayoutProps[K]>Merged into what was set before; setting the same values again does nothing.

LayoutProps and NamedLayoutProps come from layoutProps and namedLayoutProps in your InertiaConfig (see Types); without them any object goes.

Layout props are cleared on every visit that doesn't preserve state, so each page sets its own. Layouts see the change on the next microtask, not synchronously.

In a layout, @props is built from the page's props, then the props given with the layout ([AppLayout, { title }]), then the shared layout props, then the ones set for its name: later ones win.

Examples ​

A page sets the layout's title ​

gts
// layouts/AppLayout.gts
import Component from '@glimmer/component';
import type { LayoutSignature } from 'inertia-ember';

export default class AppLayout extends Component<LayoutSignature<{ title?: string }>> {
  <template>
    <header><h1>{{@props.title}}</h1></header>
    <main>{{yield}}</main>
  </template>
}
gts
// pages/Inbox.gts
import Component from '@glimmer/component';
import type Owner from '@ember/owner';
import { type PageSignature, setLayoutProps } from 'inertia-ember';

export default class Inbox extends Component<PageSignature<{ unread: number }>> {
  constructor(owner: Owner, args: PageSignature<{ unread: number }>['Args']) {
    super(owner, args);
    setLayoutProps({ title: `Inbox (${args.props.unread})` });
  }

  <template>…</template>
}

Reach one named layout ​

A page that exports named layouts can address each one:

gts
// pages/Settings.gts
import AppLayout from '../layouts/AppLayout.gts';
import SidebarLayout from '../layouts/SidebarLayout.gts';

export const layout = { app: AppLayout, sidebar: SidebarLayout };
ts
setLayoutProps('sidebar', { collapsed: true }); // only SidebarLayout gets `@props.collapsed`

The layout receives its name as @name.

Type layout props ​

ts
declare module '@inertiajs/core' {
  export interface InertiaConfig {
    layoutProps: { title: string };
    namedLayoutProps: { sidebar: { collapsed: boolean } };
  }
}

setLayoutProps({ title: 'Inbox' });
setLayoutProps('sidebar', { collapsed: 'yes' }); // type error: collapsed is a boolean

resetLayoutProps ​

Clears every prop setLayoutProps set, shared and named. Inertia already does this on each visit that doesn't preserve state; call it to clear them sooner.

Signature ​

ts
function resetLayoutProps(): void;

Examples ​

ts
import { resetLayoutProps } from 'inertia-ember';

resetLayoutProps(); // the layouts go back to the page's props

Related: Pages and layouts, TypeScript and Glint, Types, router, Hooks.

Released under the MIT License.