简体   繁体   English

当我从页面来回移动时,它不会发送发布请求

[英]When I go back and forth from the page it doesn't send post requests

I have a component where I list the policies.我有一个列出策略的组件。 For example, when I come to the payment screen of the "A" policy, I send a post request.例如,当我来到“A”保单的付款屏幕时,我发送了一个 post 请求。 I can view the installments of policy "A".我可以查看政策“A”的分期付款。 Then i go back by pressing back button in browser and go to the payment screen of policy "B".然后我按浏览器中的后退按钮返回并转到政策“B”的付款屏幕。 I see the installments of the "A" policy on the screen that comes up.我在出现的屏幕上看到了“A”政策的分期付款。 If we refresh the page it send a post request and I can view the installments of policy "B".如果我们刷新页面,它会发送一个发布请求,我可以查看政策“B”的分期付款。 How do I send the post request without refreshing the page?如何在不刷新页面的情况下发送发布请求?

service服务

const getAsosPaymentInfo = (getPolicyAccountInfoRequestDto) => {
  return new Observable((observer) => { 
    axiosInstance
      .post(SERVICE_PATH + '/getAsosPaymentInfo', getPolicyAccountInfoRequestDto)
      .then((response) => {
        observer.next(response.data);
        observer.complete();
      })
      .catch((err) => {
        console.log(err);
      });
  });
};

component零件

const { paymentInfoData, setPaymentInfoData } = useContext(PaymentInfoDataContext);

useEffect(() => {
  if (Object.keys(paymentInfoData).length === 0 && props.location.state.policy.isActive === true) {
    PolicyService.getAsosInstallments(
      {
        policyNumber: props.location.state.policy.policyNumber,
        renewalNumber: props.location.state.policy.renewalNumber,
        policyTypeExtCode: props.location.state.policy.policyType.extCode,
        productTypeExtCode: props.location.state.policy.productType.extCode
      }
    ).subscribe(
      (response) => {
        setPaymentInfoData(response);
      }
    );
  }
}, []);

For performance optimization, React Navigation avoids mounting the screen every time the user revisited it.为了性能优化,React Navigation 避免了每次用户重新访问时都挂载屏幕。 you need to listen to the screen focus event and then run your logic there.您需要收听屏幕焦点事件,然后在那里运行您的逻辑。

React Navigation provides a hook which work with normal useEffect React Navigation 提供了一个与正常useEffect工作的钩子


import {useIsFocused, useNavigation} from '@react-navigation/native';

// Some where inside screen

const { paymentInfoData, setPaymentInfoData } = useContext(PaymentInfoDataContext);

const isFocused = useIsFocused()
useEffect(() => {
  if (Object.keys(paymentInfoData).length === 0 && props.location.state.policy.isActive === true) {
    PolicyService.getAsosInstallments(
      {
        policyNumber: props.location.state.policy.policyNumber,
        renewalNumber: props.location.state.policy.renewalNumber,
        policyTypeExtCode: props.location.state.policy.policyType.extCode,
        productTypeExtCode: props.location.state.policy.productType.extCode
      }
    ).subscribe(
      (response) => {
        setPaymentInfoData(response);
      }
    );
  }
}, [isFocused]);

暂无
暂无

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

相关问题 使用 Ajax 加载页面时,返回按钮不起作用 - When load page with Ajax, the go back button doesn't works 我的ASP.NET页如何在客户端代码和服务器代码之间来回切换几次? - How can my ASP.NET page go back and forth from client to server code and back several times? 当我回到初始页面时,按钮不起作用 - Button doesn't work when I'm back on starter page 回到上一步为什么不显示v-show? - Why doesn't v-show displayed when I go back to the previous step? 当我在 vuejs 中单击它时,如何将 div 中的文本从“暗”和“亮”来回切换? - How do I switch text in a div from 'dark' and 'light' back and forth when i click it in vuejs? 调整范围时如何防止 html 按钮来回移动 slider - How to prevent html buttons from moving back and forth when I adjust a range slider 为什么当我 go 到不同的页面时,幻灯片打开菜单的按钮不起作用,但在主页上它起作用了? - Why doesn't the button for a slide open menu work when i go to a different page but on the main page it works? 如果我在屏幕上来回走动 go,为什么 useState 的 state 会突然恢复为 false? - Why the state of a useState snaps back to false if I go back and forth on the Screen? 我正在使用JqTouch,并且当我重新加载不是主页的页面时,ang会返回到主页 - Im using JqTouch, and when i reload a page which isn't homepage, ang go back, it goes to home page 如何使转换来回一次 - How to make transition go back and forth once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM