React接入Sentry.js
Posted 星陨的菲
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React接入Sentry.js相关的知识,希望对你有一定的参考价值。
// 需要安装的库
yarn add raven-js --save
yarn add raven-for-redux --save
// 在创建store的地方
import * as Raven from "raven-js";
import * as createRavenMiddleware from "raven-for-redux";
Raven.config("dns", {
release: `${版本号}`
}).install();
// 捕获saga抛出的异常
const onError = (error: Error) => {
Raven.captureException(error);
}
// 修改saga的middleware
const sagaMiddleware = createSagaMiddleware({
onError
});
const composedEnhancers = compose(
applyMiddleware(
...
createRavenMiddleware(Raven, {
getUserContext(state){
// 设置用户信息,用于上报识别
let userContext = [
"device_id": localStorage.getItem("device_id")
];
if(localStorage.getItem("uid")){
userContext["uid"] = localStorage.getItem("uid");
userContext["key"] = localStorage.getItem("key");
userContext["time"] = localStorage.getItem("time");
}
}
})
)
);
// 打开页面时,需要设置设备标识
localStorage.setItem("device_id", uuid);
// 在用户登录后,需要记录uid key time
localStorage.setItem("uid", uid);
localStorage.setItem("key", key);
localStorage.setItem("time", time);
以上是关于React接入Sentry.js的主要内容,如果未能解决你的问题,请参考以下文章