- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有一种方法可以重定向路由或返回带有数据的 Response
并使用 loader
函数在另一个页面获取它。
基本上我正在尝试创建一个带有表单的新对象并重定向到另一个我想显示创建成功消息的页面。
这是一个表单页面示例:
我正在尝试在 Response
正文中发送消息。
import { ActionFunction, Form } from "remix";
export const action: ActionFunction = async ({ request }) => {
// const formData = await request.formData();
return new Response(JSON.stringify({ message: "Hello world!" }), {
status: 303,
headers: {
Location: "/new-page",
},
});
};
export default function Index() {
return (
<div>
<Form method="post">
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</Form>
</div>
);
}
在 NewPage
中,我需要知道是否有办法在重定向响应中获取消息。
import { ActionFunction } from "remix";
export const action: ActionFunction = async ({ request }) => {
const formData = await request.formData();
// Get message here
return {
message: "",
};
};
export default function NewPage() {
return <div>New Page</div>;
}
最佳答案
这是 session flash 消息的一个很好的用例 😎
https://remix.run/docs/en/v1/api/remix#sessionflashkey-value
文档提供了一个很好的例子,但背后的想法是:
//sessions.server.ts
import { createCookieSessionStorage } from "remix";
// https://remix.run/docs/en/v1/api/remix#createcookiesessionstorage
const { getSession, commitSession, destroySession } =
createCookieSessionStorage({
cookie: {
name: "__session",
secrets: ["r3m1xr0ck5"], // should be a process.env.MY_SECRET
sameSite: "lax",
},
});
import { ActionFunction, Form } from "remix";
import { getSession, commitSession } from "./sessions";
export const action: ActionFunction = async ({ request }) => {
// const formData = await request.formData();
session.flash("myMessageKey", "Hello world!");
return redirect("/new-page", {
headers: {
"Set-Cookie": await commitSession(session),
},
});
};
export default function Index() {
return (
<div>
<Form method="post">
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</Form>
</div>
);
}
import { LoaderFunction } from "remix";
import { getSession, commitSession } from "./sessions";
export const loader: LoaderFunction = async ({ request }) => {
const formData = await request.formData();
// Get message here
const session = await getSession(
request.headers.get("Cookie")
);
const message = session.get("myMessageKey") || null;
return json(
{ message },
{
headers: {
"Set-Cookie": await commitSession(session), //will remove the flash message for you
// "Set-Cookie": await commitSession(session, { maxAge: SESSION_MAX_AGE }), //re set max age if you previously set a max age for your sessions.
},
}
);
};
export default function NewPage() {
const { message } = useLoaderData();
return <div>New Page {message}</div>;
}
关于remix.run - 重定向路由并显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71441953/
/~/@ReMix-Run/Router/dist/router.cjs.js模块分析失败:/HOME/sharib/Desktop/Full Stack Developer Tutorial/MER
我想知道是否有一种方法可以重定向路由或返回带有数据的 Response 并使用 loader 函数在另一个页面获取它。 基本上我正在尝试创建一个带有表单的新对象并重定向到另一个我想显示创建成功消息的页
我尝试使用 Remix 将合约部署到测试网(所有测试网都返回相同的消息),但出现此错误: Gas estimation errored with the following message (see
当我点击 remix.ethereum.org 下的创建时,我不明白智能合约的实际去向。例如,如果我选择 Injected Web3,这应该会将合约发布到 ropsten 测试网上,对吧?但是我怎样才
我是 Solidity 新手,这是我正在测试和重新混合的代码 browser/Untitled.sol:1:1: : 源文件未指定所需的编译器版本!考虑添加“pragma Solidity ^0.4.
默认情况下,Remix.run运行时似乎使用3000端口进行本地开发: ❯ npm run dev > dev > concurrently "npm run dev:css" "remix dev"
我需要从一个非常慢且很少更改的 API 中获取数据,因此我想我会使用内存缓存。我首先尝试了一种非常简单的方法,将其保存到我的路由中加载程序函数范围之外的变量中: let cache; export c
我需要从一个非常慢且很少更改的 API 中获取数据,因此我想我会使用内存缓存。我首先尝试了一种非常简单的方法,将其保存到我的路由中加载程序函数范围之外的变量中: let cache; export c
大家好,我是皮汤。周五翻 Github 趋势榜看到了 Remix 这个内容,觉得挺有发展前景的,初步了解了一下具体的特性,分享给大家。 近期,由 React Router 原班团队打造,基
我想不出一种方法将空字节传递给 remix 上的 solidity 函数。这是我的功能 function checkEmptyBytes(bytes calldata _data) exter
我有这个错误“类型错误:返回参数类型地址不能隐式转换为预期类型(第一个返回变量的类型)应付地址。--> Driven.sol:233:16: | 233 | return msg.sender; |
我在 Remix OS 中保存和退出 vi 编辑器时遇到问题。我在 Remix 中更改了/system/etc/init.sh 中的一些配置。此外,当我想通过单击 (Esc) 保存并退出时,然后是 :
我遵循了以下示例:https://github.com/mui-org/material-ui/tree/master/examples/remix-with-typescript在 Remix 上使
我在 remix run 中派发我的 action 时遇到了一些麻烦 - 我有一个 Aside,其中包含我购物车中的所有数据 - 我有一个表格可以整理所有数据 - 以及我想要结帐的时间待创建我想调用该
这可能是我遗漏的一个简单错误,但我终究无法弄清楚如何在此契约(Contract)中设置 msg.value 变量。我在网上看到这个值是与交易关联的wei的数量,但是我作为合约的调用者具体怎么设置这个值
我阅读了关于 Action 的 Remix 文档,我可以找到的关于 Action 的大部分信息是它使用带有按钮提交的表单 POST 来触发 Action export default function
我正在研究基于 solidity 的智能合约,但遇到了一个问题。每次我尝试创建这个合约时,我的论点都没有得到证实。 我希望在选择名称时出现“OreOreCoin”,但我得到的却是一个空字符串。 和 这
我对 Remix.run 很感兴趣,但有一件事我真的不明白。在框架的技术解释中,它说: While Remix runs on the server, it is not actually a ser
我已经在 Remix IDE 中编译了 openzeppelin-contracts 代码。同时,我收到以下警告。 Warning: The "extcodehash" instruction is
想在不安装深度操作系统的情况下获得最新的深度桌面体验吗?UbuntuDDE Remix 21.10 终于登场了! 深度 20.3 已经发布了一段时间,做了一些基本的改进和细微的视觉变化。 但
我是一名优秀的程序员,十分优秀!