【问题标题】:Using getInitialProps with HOC which also contains getInitialProps将 getInitialProps 与还包含 getInitialProps 的 HOC 一起使用
【发布时间】:2019-04-26 14:13:02
【问题描述】:

我用 withAuth HOC 包装了一个页面。在同一页面中,我还尝试调用 getInitialProps。如果我登录,getInitialProps 函数不会运行(尽管它表明我的 withAuth HOC 正在运行)。

无论如何我可以让联系页面的 getInitialProps 工作并使用 withAuth HOC(它也有一个 getInitialProps 功能)?

联系方式

import Layout from "../components/Layout";
import { withAuth } from "../services/withAuth";

class Contact extends React.Component {
  static async getInitialProps(ctx) {
    console.log("@contact authenticated ", ctx);
    return {};
  }
  render() {
    return (
      <Layout>
        <p>hey there</p>
        <a href="mailto:abc@gmail.com">abc@gmail.com</a>
      </Layout>
    );
  }
}

export default withAuth(Contact);

withAuth HOC

import { ME } from "../graphql/queries";
import redirect from "../lib/redirect";

export const withAuth = C => {
  class AuthComponent extends React.Component {
    static async getInitialProps(ctx) {
      const response = await ctx.apolloClient.query({ query: ME });
      console.log("@withAuth ", response);
      if (!response || !response.data || !response.data.me) {
        redirect(ctx, "/");
        return {
          me: null
        };
      }

      return {
        me: response.data.me
      };
    }

    render() {
      return <C {...this.props} />;
    }
  }

  return AuthComponent;
};

【问题讨论】:

    标签: reactjs next.js


    【解决方案1】:

    尝试在 HOC 的 getInitialProps 中调用 C.getInitialProps()

    export const withAuth = C => {
      class AuthComponent extends React.Component {
        static async getInitialProps(ctx) {
          const response = await ctx.apolloClient.query({ query: ME });
    
          console.log("@withAuth ", response);
    
          if (!response || !response.data || !response.data.me) {
            redirect(ctx, "/");
            return {
              me: null
            };
          }
    
          // Get component’s props
          let componentProps = {}
          if (C.getInitialProps) {
            componentProps = await C.getInitialProps(ctx);
          }
    
          return {
            me: response.data.me,
            ...componentProps
          };
        }
    
        render() {
          return <C {...this.props} />;
        }
      }
    
      return AuthComponent;
    };
    

    我希望这会有所帮助。

    【讨论】:

    • 谢谢史蒂夫!我在复制 ctx 时错过了 await 关键字。感谢您的意见。我还了解到我不能将整个 ctx 对象传递给返回值,因为它会引发循环引用错误。如果我解构 ctx 并仅传递解构后的值,它就可以正常工作。感谢您的分享
    • 0 我不明白为什么您的 getInitialProps 在返回的内部类中的 hoc 中(可能不在“/pages”中)会触发。我知道您可以传入您的 组件,然后以编程方式触发该页面的 getInitialProps - 但是将该逻辑放入 HOC 的 getInitialProps 意味着它永远不会触发。我在这里错过了什么?
    • @NewbieAid getInitialProps 将在从pages/ 目录导出的任何组件上调用。 pages/ 中的 Contact 组件不会直接导出。它用withAuth() 包裹,所以它实际上是导出的AuthComponent。因此,服务器将在 AuthComponent 上调用 getInitialProps,然后将 props 传递给包装的组件 Contact
    • @SteveHolgado 很有趣。之前没有真正考虑过composewithData HOC'ing。因此,如果您要从页面导出组件,但它包含在withDatawithAuth 等中,那么它是/hocs/with-data 的结果实际上 导出的?也许我需要多读一下compose(...
    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 2019-07-09
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多