【问题标题】:Add Google Analytics to Nextjs app with next-redux-wrapper使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序
【发布时间】:2022-01-20 03:14:26
【问题描述】:

我用 redux 包装器创建了一个 nextjs 应用程序

class MyApp extends React.Component<AppProps> {
   
    public static getInitialProps = wrapper.getInitialAppProps(store => async ({ Component, ctx }) => {

我现在正在尝试将谷歌分析集成到应用程序 (G4)。尽管许多文档解释了如何使用 useEffect 添加,但指南在应用扩展组件而不使用钩子时如何使用它们几乎没有脱节。

我需要使用 componentMount 方法还是存在更好的方法,例如将此实现升级为功能组件?

【问题讨论】:

    标签: reactjs redux react-redux react-hooks next.js


    【解决方案1】:

    将应用程序转换为功能组件并使用useEffect

      const MyApp = (AppProps) => {
          const router = useRouter();
          useEffect(() => {
    

    然后

    MyApp.getInitialProps = wrapper.getInitialAppProps(store => async ({ Component, ctx }) => {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 2020-05-16
      • 2021-11-06
      • 1970-01-01
      • 2021-01-16
      • 2022-10-14
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多