简体   繁体   English

在Relay.setVariables之后未定义变量

[英]Variables are undefined after Relay.setVariables

I have a root container which has some relayVariables : 我有一个包含一些relayVariables的根容器:

export default Relay.createContainer(UsageView, {
  initialVariables: {
    start: null,
    end: null,
  },
  fragments: {
    viewer: ({ start, end }) => Relay.QL`
      fragment on viewer {
        ${ProductList.getFragment('viewer', { start, end })}
      }
    `,
  },
});

These variables are passed down to the ProductList component and are used in it's fragment as well: 这些变量向下传递到ProductList组件,并在其片段中使用:

export default Relay.createContainer(Component, {
  initialVariables: {
    start: null,
    end: null,
  },
  fragments: {
    viewer: ({ start, end }) => Relay.QL`
      fragment on viewer {
        id
        organization {
          createdAt
          products (start: $start, end: $end) {
            name
            usageTotal
            ${ProductListItem.getFragment('data', { start, end })}
          }
        }
      }
    `,
  },
});

As you can see, these are then passed down to another component ProductListItem . 如您所见,然后将它们向下传递到另一个组件ProductListItem

All data is fetched, loaded, and rendered just fine the first time the page is loaded . 第一次加载页面时,所有数据都可以被获取,加载和呈现 I have a dropdown which allows the user to change the start and end values, which proceeds to call relay.setVariables in the parent container: 我有一个下拉菜单,允许用户更改开始结束值,该继续调用父容器中的relay.setVariables

setDateRange = ({ start, end }) => {
  this.props.relay.setVariables({
    start,
    end,
  }, (state) => {
    console.log('READYSTATE CHANGE', state);
  });
}

When this function is called, the onReadyStateChange is called 4 times, with this sequence of messages: 调用此函数时, onReadyStateChange以下消息顺序调用onReadyStateChange 4次:

  1. "NETWORK_QUERY_START" “NETWORK_QUERY_START”
  2. "CACHE_RESTORE_START" “CACHE_RESTORE_START”
  3. "CACHE_RESTORE_FAILED" “CACHE_RESTORE_FAILED”
  4. "NETWORK_QUERY_RECEIVED_ALL" “NETWORK_QUERY_RECEIVED_ALL”

After this completes, the values of start and end are undefined , causing everything to break. 完成此操作后, undefined startend的值,从而导致一切中断。

I can't seem to figure out why this is happening, as the values being set to start and end in setVariables are valid. 我似乎无法弄清楚为什么会这样,因为在setVariables中设置为startendsetVariables是有效的。

I realized I was not using the input of the setDateRange function correctly -- It was being passed a JSON string and needed to be parsed. 我意识到我没有正确使用setDateRange函数的输入-正在传递JSON字符串,需要对其进行解析。 Unfortunately, there was no way to tell from the relay request that this was the issue... 不幸的是,无法从中继请求中得知这是问题所在...

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

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