简体   繁体   English

在页面重新加载后进行突变时响应未更新但最初正在更新我可以在.network选项卡中看到

[英]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.

相关问题 GraphQL 突变仅在我重新加载页面时返回 null - GraphQL Mutation returning null only when I reload the page Material-UI 选项卡指示器在我重新加载页面时返回到第一个选项卡项 - Material-UI tab indicator returns back to the first tab item when i reload the page 为什么在使用 NextJS SSR 时在浏览器中看不到网络选项卡请求? - Why do I not see network tab requests in the browser when using NextJS SSR? 为什么在 React 中使用输入响应动态更改内容后,我的内容没有更新? - Why is my content not being updated after I dynamically change it with input response in React? 更新上下文 React 后子组件未更新 - Child Components not being updated after updating context React 突变后等待缓存数据重新加载的模式 - Pattern for waiting for cache data to reload after a mutation 如何修复“重新加载页面后更改选项卡”。 - How can I fix "Change tab after reloading page ." Apollo Mutation - 在 useMutation 更新后 UI 未更新 - Apollo Mutation - UI not updated after useMutation update GraphQL 变异后的 refetchQueries,this.props 未更新 - GraphQL refetchQueries after mutation, this.props not updated Javascript/Angular:在网络选项卡中,我如何在成功登录我的应用程序后隐藏登录 API 调用? - Javascript/Angular : In Network tab how can I hide login API call after successfully login in my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM