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