如何在React中执行beforeunload时获取State对象

huangapple go评论67阅读模式
英文:

How get get State objects when performing beforeunload in React

问题

以下是您要翻译的内容:

我有一个对象我试图在 beforeunload 事件中保存到一个会话持续的数组中

const { serviceRequest, serviceRequestOriginal, isDirty } = useAppSelector((state) => state.csmRequestDataReducer);

useEffect(() => {
  
  window.addEventListener("beforeunload", (event) => {
    console.log('beforeunload:' + isDirty);
    console.log(current(serviceRequest));
    console.log('beforeunload:' + isDirty);
    dispatch(persistRequestTab({ currentSRCurrentState: isDirty ? serviceRequest : null, currentSRPreviousState: isDirty ? serviceRequestOriginal : null }));
    console.log("页面重新加载前的API调用");
  });
  
}, []);

然而控制台输出显示 serviceRequest = null  isDirty = false

为什么会这样我该如何更改/修复以获取这些对象/
英文:

I have this object that I'm trying to save to an session persisted array with the beforeunload event

  const { serviceRequest, serviceRequestOriginal, isDirty } = useAppSelector((state) => state.csmRequestDataReducer);

  useEffect(() => {
    
    window.addEventListener("beforeunload", (event) => {
      console.log('beforeunload:' + isDirty);
      console.log(current(serviceRequest));
      console.log('beforeunload:' + isDirty);
      dispatch(persistRequestTab({ currentSRCurrentState: isDirty ? serviceRequest : null, currentSRPreviousState: isDirty ? serviceRequestOriginal : null }));
      console.log("API call before page reload");
    });
    
  }, []);

However the console output shows serviceRequest = null and isDirty = false.

Why is this and can I alter/fix to get these objects/values?

答案1

得分: 0

好的,以下是翻译的部分:

好的我不确定这是否是一个完美的解决方案但它似乎运行得很好

在创建事件监听器时它锁定了初始状态对象的值使用 useRef() 可以解决这个问题只需确保随着状态的更新更新引用使用一些额外的 useEffect

const { serviceRequest, serviceRequestOriginal, isDirty } = useAppSelector((state) => state.csmRequestDataReducer);
const currentSRRef = useRef<ServiceRequest | null>(serviceRequest);
const currentSROrigRef = useRef<ServiceRequest | null>(serviceRequestOriginal);
const currentIsDirtyRef = useRef<boolean>(isDirty);

useEffect(() => {
  
  window.addEventListener("beforeunload", (event) => {
    console.log('beforeunload:' + currentIsDirtyRef.current);
    dispatch(persistRequestTab({ currentSRCurrentState: currentIsDirtyRef.current ? currentSRRef.current : null, currentSRPreviousState: currentIsDirtyRef.current ? currentSROrigRef.current : null }));
  });
  
}, []);

useEffect(() => {
  currentSRRef.current = serviceRequest;
  currentIsDirtyRef.current = isDirty;
}, [serviceRequest])

useEffect(() => {
  currentSROrigRef.current = serviceRequestOriginal;
}, [serviceRequestOriginal])
英文:

Ok so I don't know if this is a perfect resolution however it seems to work really well.

When creating the event listener it locks in the initial state objects values, using a reference useRef() will get around this, just make sure to update the reference as state is updated with some more useEffects.

  const { serviceRequest, serviceRequestOriginal, isDirty } = useAppSelector((state) =&gt; state.csmRequestDataReducer);
  const currentSRRef = useRef&lt;ServiceRequest | null&gt;(serviceRequest);
  const currentSROrigRef = useRef&lt;ServiceRequest | null&gt;(serviceRequestOriginal);
  const currentIsDirtyRef = useRef&lt;boolean&gt;(isDirty);
  
  useEffect(() =&gt; {
    
    window.addEventListener(&quot;beforeunload&quot;, (event) =&gt; {
      console.log(&#39;beforeunload:&#39; + currentIsDirtyRef.current);
      dispatch(persistRequestTab({ currentSRCurrentState: currentIsDirtyRef.current ? currentSRRef.current : null, currentSRPreviousState: currentIsDirtyRef.current ? currentSROrigRef.current : null }));
    });
    
  }, []);

  useEffect(() =&gt; {
    currentSRRef.current = serviceRequest;
    currentIsDirtyRef.current = isDirty;
  }, [serviceRequest])

  useEffect(() =&gt; {
    currentSROrigRef.current = serviceRequestOriginal;
  }, [serviceRequestOriginal])

huangapple
  • 本文由 发表于 2023年2月16日 06:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定