[英]response not updating when doing mutation after page reload but originally being updated which i can see in network tab
Here, i am trying to add "view" on mutation.在这里,我试图在突变上添加“视图”。 the first view created after a reload runs into this issue.
重新加载后创建的第一个视图遇到了这个问题。 If you get the 404 page and hit the back button, it seems to work right the second time.
如果您获得 404 页面并点击后退按钮,它似乎第二次工作正常。
Here's what happens, big picture (it happens too fast to see without pausing with debugger): 1.send mutation 2.get result 3.setViewId is called, which updates the URL query string 4.page is rendered, hits a check for the view based on ID.这是发生的事情,大图(它发生得太快,不暂停调试器就看不到): 1.send mutation 2.get result 3.setViewId 被调用,更新 URL 查询字符串 4.page 被呈现,点击检查基于ID的视图。 If it's not in the show's list of views, redirect to /not-found (404 page).
如果它不在节目的视图列表中,则重定向到 /not-found(404 页面)。
The last one is where it gets weird.最后一个是它变得奇怪的地方。 When debugging, the shows include the new view in the.network tab …but finding the view by ID returns undefined because the response i am getting from the useQuery does not update the record after page reload.
调试时,显示在 .network 选项卡中包含新视图……但按 ID 查找视图返回未定义,因为我从 useQuery 获得的响应在页面重新加载后不会更新记录。 I have no idea why at this point.
我现在不知道为什么。
creating viewId and setViewId创建 viewId 和 setViewId
const viewId = Number(searchParams.get('viewId')) || undefined;
const setViewId = useCallback<(viewId: number, replace?: boolean) => void>(
(id: number, replace = false) => {
navigate(route(`?viewId=${id}`), { replace });
setViewZoneId(undefined);
},
[navigate, route],
);
// If view id is not set, set it
useEffect(() => {
if (viewId == null) setViewId(show.defaultViewId, true);
}, [setViewId, show.defaultViewId, viewId]);
// If there's a view but view zone id isn't set, set it
useEffect(() => {
if (viewId == null || viewZoneId != null) return;
const view = show.views.find(({ id }) => id === viewId);
if (view == null) return;
setViewZoneId(view.viewZones[0].id);
}, [show.views, viewId, viewZoneId]);
// end hooks
if (viewId == null) return null;
const view = show.views.find(({ id }) => id === viewId);
if (view == null) return <Navigate to={route('/not-found')} replace />;
if (viewZoneId == null) return null;
passing setViewId into Component将 setViewId 传递给组件
<CreateViewWizard {...wizard.props} show={show} setViewId={setViewId} />
here doing mutation and setting viewId in CreateViewWizard这里在 CreateViewWizard 中进行突变和设置 viewId
<Button
color="primary"
disabled={(step === 0 && name.trim() === '') || step > 2}
onClick={async () => {
setStep((prevStep) => prevStep + 1);
if (step < 2) return;
close();
assert(typeof layoutId === 'number', 'Layout not set');
const { data } = await createView({ variables: { layoutId, name, showId: show.id } });
if (data?.createView?.view != null) setViewId(data.createView.view.id);
}}
variant="contained"
>
{step === 2 ? 'Create' : 'Next'}
</Button>
If viewId
is a string type and id
is a number type, well, these can never be strictly equal ( ===
), eg "5" === 5
is always false.如果
viewId
是字符串类型而id
是数字类型,那么,它们永远不会严格相等 ( ===
),例如"5" === 5
始终为 false。 Try a type-safe comparison, eg "5" === String(5)
.尝试类型安全的比较,例如
"5" === String(5)
。
Example:例子:
console.log('5 === "5"', 5 === "5"); // false console.log('String(5) === "5"', String(5) === "5"); // true
Your code:你的代码:
const view = show.views.find(({ id }) => String(id) === viewId);
Keep in mind that Array.prototype.find
also potentially returns undefined
when it doesn't find a matching element.请记住,
Array.prototype.find
在找不到匹配元素时也可能返回undefined
。 You probably should check for any truthy view
return value, and redirect when view
is falsey.您可能应该检查任何真实的
view
返回值,并在view
为假时重定向。
if (!view) {
return <Navigate to={route('/not-found')} replace />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.