加载器和amp之间共享对象;在remix.run中调用操作后,操作不会更新?

发布于 2025-01-21 23:12:35 字数 397 浏览 1 评论 0原文

我有一个模拟数据库,可以使用一些装载机和操作。

这是粗略的布局:

const db = { key: "bar" }

export const action = async ({ request }) => {
     db.key = "foo"
}

export const loader = async ({ request }) => {
    return json(db)
}

不过我有一个问题。当调用操作时,它成功更新了db.key,但是,加载程序后来被调用。该值是{键:“ bar”}。有人知道为什么再次调用加载程序时未更新对象吗?

I have a mock database for playing around with some loaders and actions.

Here's the rough layout:

const db = { key: "bar" }

export const action = async ({ request }) => {
     db.key = "foo"
}

export const loader = async ({ request }) => {
    return json(db)
}

I have an issue though. When the action is called, it successfully updates db.key, however, the loader is called afterwards & the value is {key: "bar" }. Does anyone know why the object is not updated when the loader is called again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2025-01-28 23:12:35

我认为与” data写作“ 主题”。
每当提交表单时,它都会重新加载页面上所有路由的所有数据。这就是为什么许多人访问Redux等全球州管理库的原因。

我不知道它是否有帮助,但是在这里我有一种简单的方式可以在其中发送发布请求,使用操作对其进行更新并加载与加载程序一起在URL上搜索的内容。

export const action: ActionFunction = async ({ request }) => {
  const form = await request.formData();
  const key = form.get("key");
  return redirect(`/?key=${key}_updated`);
};

export const loader: LoaderFunction = async ({ request }: any) => {
  const url = new URL(request.url);
  const key = url.searchParams.get("key");
  return json({ key });
};

export default function Index() {
  const loaderData = useLoaderData();
    
  return (
    <div>
      <span>loaderData: {loaderData.key}</span>
      <Form method="post">
        <input type="text" id="key" name="key" defaultValue={loaderData.key} />
        <button>Submit</button>
      </Form>
    </div>
  );
}

I think that there is something related to "Data Writes" topic.
Whenever a form is submitted it reload all of the data for all of the routes on the page. Thats why many people reach for global state management libraries like redux.

I don't know if it helps but here I got a simple way where you can send a post request, update it with the action and load the content searching at the URL with the loader.

export const action: ActionFunction = async ({ request }) => {
  const form = await request.formData();
  const key = form.get("key");
  return redirect(`/?key=${key}_updated`);
};

export const loader: LoaderFunction = async ({ request }: any) => {
  const url = new URL(request.url);
  const key = url.searchParams.get("key");
  return json({ key });
};

export default function Index() {
  const loaderData = useLoaderData();
    
  return (
    <div>
      <span>loaderData: {loaderData.key}</span>
      <Form method="post">
        <input type="text" id="key" name="key" defaultValue={loaderData.key} />
        <button>Submit</button>
      </Form>
    </div>
  );
}
予囚 2025-01-28 23:12:35

我在remix v2.12中重新创建了您的示例,它起作用

示例1(更新对象):

对象数组):

我怀疑这是有效的,因为自从混音V1或帖子的时间以来,热重新加载机制是不同的。在V2中,它更聪明地检测更改

其他尖端:
大多数库和框架确实通过检查对象或数组是否已更改(ex:react)来更改检测。因此,通常情况下,如果您要在内存数据结构中使用,则要重新创建整个对象/数组,至少这通常是触发更改检测的一种方法

let obj = {test: 'test'} 
obj = {test: 'updated'}

let arr = [1,2,3]
arr = [...arr,4]

I recreated your example in Remix v2.12 and it worked

Example 1 (updating object):

Example 2 (updating array of objects):

I suspect that this works because since remix v1 or the time of your post, the hot reloading mechanism was different. In v2, its smarter about detecting changes

Other tips:
Most libraries and frameworks do change detectinon by checking if the object or array has changed (ex: react). So typically, if you're using in memory data structure like that you want to recreate the entire object/array, at least that's usually 1 of the ways to trigger change detection

let obj = {test: 'test'} 
obj = {test: 'updated'}

let arr = [1,2,3]
arr = [...arr,4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文