ReactJS:如何通过嵌套的'anything()(Component)'传递变量?

时间:2017-10-30 18:19:06

标签: javascript reactjs

首先:我不知道如何调用const result = anything()(Component),所以我在搜索中找不到有用的信息。这篇文章的标题必须经过编辑......

但这是我的问题:

这是我通过react-apollo向我的Component添加数据的方法:

const result = withData(
  graphql(
    gql`
      query Content($id: ID!, $language: String!) {
        article(id: $id) {
          title,
          intro(language: $language)
          }
        }
      }
    `, {
      options: (props) => ({
        variables: {
          id: props.id,
          language: language
        }
      })
    }
  )(Component)
)

如您所见,我必须使用变量$id$language。现在我需要为我的组件添加i18n支持,我必须使用translate()(Component)

const result = translate(['common'], { i18n, wait: process.browser })(
  withData(
  graphql(
    gql`
      query Content($id: ID!, $language: String!) {
        article(id: $id) {
          title,
          intro(language: $language)
          }
        }
      }
    `, {
      options: (props) => ({
        variables: {
          id: props.id,
          language: language
        }
      })
    }
  )(Component)
  )
)

翻译工作正常,但我收到错误Unhandled (in react-apollo) Error: GraphQL error: Variable "$id" of required type "ID!" was not provided.

我做错了,因为似乎id值没有进入查询...

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

import { compose } from 'react-apollo';

...

export default compose(
  graphql(`query { ... }`),
  translate(['common'], { i18n, wait: process.browser })
)(Component);