Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remix-react): make actionData available in meta function #3534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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