简体   繁体   English

Remix中的state如何管理?

[英]How to manage the state in Remix?

I was going through Remix for quite a sometime but still not able to figure out state management.我研究 Remix 已经有一段时间了,但仍然无法弄清楚 state 管理。 How to share the data between the components/routes?如何在组件/路由之间共享数据? How to store the data which can be accessible by any component?如何存储任何组件都可以访问的数据? And should be able to update the data from any child component.并且应该能够更新来自任何子组件的数据。

First of all, remember that Remix is still React, so all the state management techniques you're used to still work with Remix.首先,请记住 Remix 仍然是 React,因此您习惯使用的所有 state 管理技术仍然适用于 Remix。

With that being said, Remix uses loaders to manage the server-side state. You can access this via useLoaderData from any component in the current route.话虽如此,Remix 使用加载程序来管理服务器端 state。您可以从当前路由中的任何组件通过useLoaderData访问它。 You're not limited to just the route file.您不仅限于路由文件。

Also, via useMatches you have access to all the loader data across your nested routes from root to leaf.此外,通过useMatches ,您可以访问从根到叶的嵌套路由中的所有加载程序数据。

Mutations should be done via actions, so instead of trying to mutate local data, POST to your action the update, and let Remix revalidate your routes to make sure your local data is in sync.突变应该通过操作来完成,所以不要试图改变本地数据,而是将更新发布到您的操作,并让 Remix 重新验证您的路由以确保您的本地数据同步。

Purely client-side state, like if a dialog is visible, etc. can still be managed with useState , etc.纯客户端 state,比如对话框是否可见等仍然可以使用useState等进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM