Skip to content

Commit

Permalink
feat: actionData available in the meta function
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-gray committed May 3, 2023
1 parent acdd7c7 commit ae304b9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
- kayac-chang
- kbariotis
- KenanYusuf
- kenneth-gray
- kentcdodds
- kevinrambaud
- kevlened
Expand Down
1 change: 1 addition & 0 deletions docs/route/meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const meta: MetaFunction = () => ({
`meta` function is passed an object that has following data:

- `data` is whatever exported by `loader` function
- `actionData` is the data returned by the `action` function
- `location` is a `window.location`-like object that has some data about the current route
- `params` is an object containing route params
- `parentsData` is a hashmap of all the data exported by `loader` functions of current route and all of its parents
Expand Down
28 changes: 28 additions & 0 deletions integration/meta-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ test.describe("meta", () => {
}
`,

"app/routes/action.jsx": js`
export const action = async () => {
return { error: true };
};
export const meta = ({ actionData }) => ({
title: (actionData && actionData.error ? 'Error: ' : '') + 'Action',
});
export default function Action() {
return (
<form method="POST">
<button type="submit">Submit</button>
</form>
);
}
`,

"app/routes/music.jsx": js`
export function meta({ data }) {
return {
Expand Down Expand Up @@ -384,6 +402,16 @@ test.describe("meta", () => {
expect(await app.getHtml("title")).toBe("<title>Blog Posts</title>");
});
});

test("meta receives actionData when available", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);

await app.goto("/action");
expect((await app.getElement("title")).text()).toEqual("Action");

await page.click("button[type=submit]");
expect((await app.getElement("title")).text()).toEqual("Error: Action");
});
});

test.describe("v2_meta", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ function V1Meta() {
errors,
matches: routerMatches,
loaderData,
actionData: actionRouteData,
} = useDataRouterStateContext();
let location = useLocation();

Expand All @@ -605,6 +606,7 @@ function V1Meta() {
for (let match of matches) {
let routeId = match.route.id;
let data = loaderData[routeId];
let actionData = actionRouteData ? actionRouteData[routeId] : null;
let params = match.params;

let routeModule = routeModules[routeId];
Expand All @@ -614,6 +616,7 @@ function V1Meta() {
typeof routeModule.meta === "function"
? (routeModule.meta as V1_MetaFunction)({
data,
actionData,
parentsData,
params,
location,
Expand Down
1 change: 1 addition & 0 deletions packages/remix-react/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface LinksFunction {
export interface V1_MetaFunction {
(args: {
data: AppData;
actionData: AppData;
parentsData: RouteData;
params: Params;
location: Location;
Expand Down

0 comments on commit ae304b9

Please sign in to comment.