首页 文章

将Apollo GraphQL HOC模式与现有HOC一起使用

提问于
浏览
0

我有一个基本的HOC,它围绕着我的一些组件:

import * as React from 'react';    
export const AuthRoute = () => (WrappedComponent: any) => {
  return class extends React.Component<{}, {}> {
    constructor(props: any) {
      super(props);
    }
    public render() {
      return <WrappedComponent {...this.props} />;
    }
  };
}

我希望这个HOC能够访问另一个HOC - 用于GraphQL的React Apollo客户端HOC . 我已经尝试在这个HOC上使用Apollo客户端,但它给出了一个错误“TypeError:无法设置未定义的属性'道具'” . 这是我的尝试:

const AuthRoute = () => (WrappedComponent: any) => {
  return class extends React.Component<{}, {}> {
    constructor(props: any) {
      super(props);
    }
    public render() {

      return <WrappedComponent {...this.props} />;
    }
  };
}

const GET_AUTH = gql`
  {
    authStatus {
      authStatus
    }
  }
`;

export default compose(graphql(GET_AUTH))(AuthRoute);

我在这里错过了一些简单的东西,因为我认为这会有用吗?或者,对于像Apollo这样具有现有HOC的HOC库,是否有更好的做法?

谢谢!

1 回答

  • 1

    看起来你刚刚得到一个错字 . 将默认导出更改为:

    export default compose(graphql(GET_AUTH), AuthRoute);

    ......它应该有效

相关问题