javascript - 无法读取未定义的属性 parentNode
问题描述
我正在使用 react-ga 从使用谷歌分析跟踪我的反应应用程序。我通过 API 调用获取跟踪 ID,大约 4 秒后返回。我的功能是这样的:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentDidMount() {
ReactGA.initialize(this.props.partnerTrackingCode);
const page = this.props.location.pathname;
trackPage(page);
}
componentWillReceiveProps(nextProps) {
console.log(nextProps);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
if (currentPage !== nextPage) {
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
export default withTracker;
问题是必须首先调用初始化函数并且使用 undefined 调用,因为起初 trackid 是未定义的(请记住,它是异步获取的)。我使用了这个编辑:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentWillReceiveProps(nextProps) {
if(nextProps.trackId) {
ReactGA.initiliaze(nextProps.trackId);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
我正在使用 react-ga 从使用谷歌分析跟踪我的反应应用程序。我通过 API 调用获取跟踪 ID,大约 4 秒后返回。我的功能是这样的:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentDidMount() {
ReactGA.initialize(this.props.partnerTrackingCode);
const page = this.props.location.pathname;
trackPage(page);
}
componentWillReceiveProps(nextProps) {
console.log(nextProps);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
if (currentPage !== nextPage) {
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
export default withTracker;
问题是必须首先调用初始化函数并且使用 undefined 调用,因为起初 trackid 是未定义的(请记住,它是异步获取的)。我使用了这个编辑:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentWillReceiveProps(nextProps) {
if(nextProps.trackId) {
ReactGA.initiliaze(nextProps.trackId);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
但我收到一个错误,无法读取未定义的属性 parentNode你有什么想法吗?
解决方案
您GoogleAnalytics.set({ username });
在拨打电话之前 是否设置了这样的用户名GoogleAnalytics.initialize
?如果不尝试这样做,看看它是否有所作为。我刚才遇到了同样的问题,这对我有用。
推荐阅读
- typescript - Typescript:如何通过泛型参数的属性和附加类型来限制属性选择?
- amazon-web-services - Terraform for_each - each.key 在哪里定义?
- php - 检索具有特殊字符的记录时 JSON 失败
- flutter - GetX Get.ToNamed 在孩子内部不起作用
- nfc - 在 JavaCard 上激活小程序
- java - 如何在我的 Recycler Views Adapter 中获得最大浮点值?
- python - 用户访问被拒绝(mysql和flask)
- drupal - 检查文件是否在段落的文件字段中上传
- django - Nginx(在 docker 上)尝试获取静态文件时返回 403 Forbiden
- c - ncurses get_wch() 函数未声明