如何解决使用 React
我在我的 React 项目中使用 Recoil (recoiljs.org)。
我有一个带有项目列表的相当经典的设置 - 对象图看起来像这样:
Routes - routes is an array of Route objects
- Route - route object is edited in a form
我使用一个原子来表示当前选择的路由数组,例如
const [routes,setRoutes] = useRecoilValue(currentRoutesViewState);
而且我还使用 selectorFamily
来允许单个控件绑定到单个项目并更新单个项目的状态。
const routeSelectorFamily = selectorFamily({
key: "routeSelectorFamily",get:
(routeKey) =>
({ get }) => {
const routes = get(currentRoutesViewState);
return routes.find((r) => r.key === routeKey);
},set:
(routeKey) =>
({ set },newValue) => {
set(currentRoutesViewState,(oldRoutes) => {
const index = oldRoutes.findindex((r) => r.key === routeKey);
const newRoutes = replaceItemAtIndex(
oldRoutes,index,newValue as Routeviewmodel
);
return newRoutes;
});
},});
如您所见,set
函数允许某人更新单个路由状态,并将其复制到任何渲染路由中。
但是,我想自动保存对整个图表的任何更改,但不知道如何轻松观察 Routes 父对象的任何更改。有没有办法以编程方式订阅更新,以便我可以序列化并保存此状态?
解决方法
您可以为此使用 v.NumField()
:
useEffect
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。