Actions and Flat Layout Routes #1178
-
Hey, I'm currently trying out Remix's features. Currently I'm having a hard time with flat layout routes & actions. Given the following routes (e.g. there is no
And these files // __app.tsx
import React from 'react';
import { Link, Outlet } from 'remix';
const AppLayout: React.FC = () => (
<div>
<header>
<h1>Dashboard</h1>
</header>
<section>
<nav>
<ul>
<li>
<Link to="/">Dashboard</Link>
<Link to="feedback">Give feedback</Link>
</li>
</ul>
</nav>
</section>
<main>
<Outlet />
</main>
</div>
);
export default AppLayout; // __app/feedback/index.tsx
import type { ActionFunction, MetaFunction, RouteHandle } from 'remix';
import React from 'react';
import { redirect } from 'remix';
import { RadioGroupRoot, RadioItem } from '../../../components/RadioGroup';
export const handle: RouteHandle = { hydrate: true };
export const meta: MetaFunction = () => ({
title: 'Feedback',
});
export const action: ActionFunction = async ({ request }) => {
const formData = await request.formData();
console.log(formData);
return redirect('/feedback');
};
const Feedback: React.FC = () => {
const [value, setValue] = React.useState('1');
React.useEffect(() => {
console.log(value);
}, [value]);
return (
<div>
<h1>How happy are you?</h1>
<form method="post">
<RadioGroupRoot value={value} onValueChange={setValue}>
<RadioItem value="1" label="Unhappy" />
<RadioItem value="2" label="It's okay" />
<RadioItem value="3" label="Happy" />
</RadioGroupRoot>
<button type="submit">Submit</button>
</form>
</div>
);
};
export default Feedback; when clicking the "Submit" button, the
If I modify // __app.tsx
import React from 'react';
import { Link, Outlet, ActionFunction, redirect } from 'remix';
export const action: ActionFunction = async ({ request }) => {
const formData = await request.formData();
console.log(formData);
return redirect('/feedback');
};
const AppLayout: React.FC = () => (
<div>
<header>
<h1>Dashboard</h1>
</header>
<section>
<nav>
<ul>
<li>
<Link to="/">Dashboard</Link>
<Link to="feedback">Give Feedback</Link>
</li>
</ul>
</nav>
</section>
<main>
<Outlet />
</main>
</div>
);
export default AppLayout; additionally, there's a server-side error
which seems to contradict the former error message. Surely I'm missing somethíng here. Thanks in advance for any pointers! :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I figured it out. It needs to be |
Beta Was this translation helpful? Give feedback.
I figured it out. It needs to be
routes/feedback.tsx
instead ofroutes/feedback/index.tsx
. This may be an opportunity to improve the error messages, if possible — it might be possible to detect such cases and let them point the user in the right direction.