简体   繁体   English

使用导入的函数在React组件上调用compose

[英]Calling compose on React components with imported functions

I am trying to call several functions on a React component I have created, specifically the graphql() function from react-apollo 我正在尝试在我创建的React组件上调用几个函数,特别是来自react-apollographql()函数

I use the pattern: 我使用模式:

import someFunction from "../somewhere";

const withData = compose(
  graphql(gqlDocument, { name: "Name" }),
  graphql(anotherDocument, { name: "Name2" }),
  someFunction()
  ...
)
const ComponentWithData = withData(Component);
export default ComponentWithData

This works fine, unless I try to use a function that I have defined in another file and imported. 除非我尝试使用在另一个文件中定义并导入的函数,否则此方法效果很好。 despite literally being of the exact same form, ie graphql(document, { name: "Name" }) I get the error: Uncaught TypeError: Cannot read property 'apply' of undefined . 尽管实际上是完全相同的形式,即graphql(document, { name: "Name" })我收到错误: Uncaught TypeError: Cannot read property 'apply' of undefined If I simply copy and paste the function from the external module and put it inside the compose block, it works perfectly, so the issue has to be with how react is dealing apply inside this function composition. 如果我只是复制和粘贴来自外部模块的功能,并把它撰写块内,它完美的作品,所以这个问题必须有如何反应的处理apply本函数组合里面。

When the function works, it ComponetWithData should have a prop called "Name" that represents the graphql document passed to the graphql() function. 当该函数工作时,它的ComponetWithData应该具有一个称为“名称”的属性,该属性表示传递给graphql()函数的graphql文档。 This is particularly perplexing because react-apollo's implementation of subscriptions has you passing a React component to a function that alters its props, and yet I can define subscription functions externally with no hassle. 这特别令人困惑,因为react-apollo的订阅实现使您将React组件传递给可以改变其属性的函数,但是我可以轻松地在外部定义订阅函数。 here is the pattern for reference: 这是供参考的模式:

const COMMENTS_SUBSCRIPTION = gql`
    subscription onCommentAdded($repoFullName: String!){
      commentAdded(repoFullName: $repoFullName){
        id
        content
      }
    }
`;
const withData = graphql(COMMENT_QUERY, {
    name: 'comments',
    options: ({ params }) => ({
        variables: {
            repoName: `${params.org}/${params.repoName}`
        },
    }),
    props: props => {
        return {
            subscribeToNewComments: params => {
                return props.comments.subscribeToMore({
                    document: COMMENTS_SUBSCRIPTION,
                    variables: {
                        repoName: params.repoFullName,
                    },
                    updateQuery: (prev, {subscriptionData}) => {
                        if (!subscriptionData.data) {
                            return prev;
                        }
                        const newFeedItem = subscriptionData.data.commentAdded;
                        return Object.assign({}, prev, {
                            entry: {
                                comments: [newFeedItem, ...prev.entry.comments]
                            }
                        });
                    }
                });
            }
        };
    },
});

While I didn't ever get it to work inside the compose block, I did find an acceptable solution: 尽管我从未在compose块中使用它,但确实找到了可接受的解决方案:

const myFunc = compose(
  // graphql stuff here
);
const updated = myImportedFunc(Component);
const CompWGraphql = myFunc(updated);

There was just an issue with how graphql passes the object from one function to the other in the compose chain, and simply calling it outside the compose block and assigning the result to a new variable gave me the result I wanted. graphql如何将对象从一个函数传递到组成链中的另一个函数只是一个问题,只需在compose块外调用它并将结果分配给新变量即可得到我想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM