首页 > 解决方案 > 无法读取未定义的属性 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你有什么想法吗?

标签: javascriptreactjsgoogle-analyticsreact-ga

解决方案


GoogleAnalytics.set({ username });在拨打电话之前 是否设置了这样的用户名GoogleAnalytics.initialize?如果不尝试这样做,看看它是否有所作为。我刚才遇到了同样的问题,这对我有用。


推荐阅读