如何解决在react和close弹出窗口中从eventListener调度事件
我仍然是个新手,我想在响应中调用dispatch()
,但是该页面从未停止加载,如下面的快照所示。
这是我的代码...
profile / index.jsx
import { usedispatch } from 'react-redux';
import {profileOperations} from "state/profile";
const dispatch = usedispatch();
const openPopup = () => {
const popupWindow = window.open('',POPUP_WINDOW,windowParams);
attachEvent(popupWindow);
};
const attachEvent = popupWindow => {
window.addEventListener(
'message',function(event){
if (event.data &&
typeof event.data !== 'undefined') {
dispatch(profileOperations.saveProfile(event));
popupWindow.close();
}
},false
);
);
const windowParams = () => {
const threeDSPopupHeight = 500;
const threeDSPopupWidth = isMobileDevice ? 360 : 600;
const popupLeftPosition = leftPosition(threeDSPopupWidth);
const popupTopPosition = topPosition(threeDSPopupHeight);
return `resizable=yes,toolbar=no,screenX=${popupLeftPosition},screenY=${popupTopPosition},menubar=no,scrollbar=yes,width= ${threeDSPopupWidth},height=${threeDSPopupHeight},top= ${popupTopPosition},left=${popupLeftPosition}`;
};
profile / operations.js
export const saveProfile = event => dispatch => {
try {
const response = JSON.parse(event.data);
if (response.profiles) {
dispatch(processprofile(response));
} else if(response.statusCode){
console.log('body contains error');
dispatch(resourceErrorHandler(response))
}
} catch {
// do nothing
}
}
这是服务器端的示例响应。我通过将以下代码中的profile
或error
变量传递给客户端进行测试:
handleCallback.js
module.exports = ({ error,response = {},body },res) => {
const profile = '{"profiles":[{"id":"515592671","token":"8416003642718270"}]}';
const error = '{"message": "Internal Error with browser","statusCode": 500,"code": 1004}';
res.send("<script type=\"text/javascript\">"
+ "const data = "+JSON.stringify(profile)+";"
+ " window.opener.postMessage(data,\"http://localhost\");"
+ "</script>");
};
成功:
{
"profiles": [{
"id": "515592671","token": "8416003642718270"
}]
}
错误:
{
"message": "Internal Error with browser","code": 1004
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。