【问题标题】:React Apollo graphql enricher not workingReact Apollo graphql 浓缩器不工作
【发布时间】:2019-12-10 07:09:59
【问题描述】:

由于某种原因,graphql 高阶函数没有按预期运行,特别是查询甚至没有被调用。这就是我现在拥有的:

function viewReducer({ dispatch, value }) {
    let temp = () => (<MyPage
                          onReconnect = {() => {dispatch(restartSetup());}}
                      />);
    let View = graphql(myQuery)(temp);
    return <View />
}

export { viewReducer }; 

我到底错过了什么?

graphqlreact-apollo 文档。

编辑:经过一些调试,看起来 props 没有传递给实际组件。更新的调试代码:

function viewReducer({ dispatch, value }) {
    let temp = () => (<MyPage
                          onReconnect = {() => {dispatch(restartSetup());}}
                      />);
    let View = graphql(myQuery,
                    options: {
                        fetchPolicy: 'cache-and-network'
                    },
                    props: (props) => {
                        console.log("[INFO] " + JSON.stringify(props))
                        return props;
                    }               
               )(temp);
    return <View />
}

我可以看到获取的数据,但没有传递道具,它仍然是未定义的。

【问题讨论】:

  • 你如何确定查询“没有被调用”?您没有在网络标签中看到触发的请求吗?
  • 在触发查询之前,它会获取 AWS Cognito 凭证以进行身份​​验证。它是一个自定义凭据获取器。如果我直接使用 标记,我看不到日志。
  • 所以,我注意到了一些事情。请查看更新后的问题。
  • 啊,有问题了。看答案。

标签: javascript reactjs graphql redux-saga react-apollo


【解决方案1】:

已修复。

问题出在这里:

let temp = () => (<MyPage
                          onReconnect = {() => {dispatch(restartSetup());}}
                      />);

由于这个闭包不接受 props 作为参数,所以它按原样执行和渲染。为了解决这个问题,我只是添加了道具作为参数:

let temp = ({data}) => (<MyPage
                          onReconnect = {() => {dispatch(restartSetup());}}
                          data = {data}
                      />);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2018-04-12
    • 2018-12-27
    • 2019-06-19
    • 1970-01-01
    • 2017-09-01
    • 2020-02-14
    • 2021-12-22
    相关资源
    最近更新 更多