我正在使用react-ga通过Google Analytics(分析)跟踪我的React应用。我正在通过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;
问题在于,必须首先调用initiliaze函数,并使用undefined对其进行调用,因为起初,trackid是undefined(请记住以异步方式获取)。我使用了此编辑:
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 你有什么想法做什么?