加载器和amp之间共享对象;在remix.run中调用操作后,操作不会更新?
我有一个模拟数据库,可以使用一些装载机和操作。
这是粗略的布局:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为与” data写作“ 主题”。
每当提交表单时,它都会重新加载页面上所有路由的所有数据。这就是为什么许多人访问Redux等全球州管理库的原因。
我不知道它是否有帮助,但是在这里我有一种简单的方式可以在其中发送发布请求,使用操作对其进行更新并加载与加载程序一起在URL上搜索的内容。
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.
我在remix v2.12中重新创建了您的示例,它起作用
示例1(更新对象):
对象数组):
我怀疑这是有效的,因为自从混音V1或帖子的时间以来,热重新加载机制是不同的。在V2中,它更聪明地检测更改
docs: https ://remix.run/docs/en/main/other-api/dev#remix-dev
视频: https //www.youtube.com/watch?v=ztrjaut9hlo
其他尖端:
大多数库和框架确实通过检查对象或数组是否已更改(ex:react)来更改检测。因此,通常情况下,如果您要在内存数据结构中使用,则要重新创建整个对象/数组,至少这通常是触发更改检测的一种方法
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
docs: https://remix.run/docs/en/main/other-api/dev#remix-dev
video: https://www.youtube.com/watch?v=zTrjaUt9hLo
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