简体   繁体   English

在setVariables之后,React relay pendingVariables始终为null

[英]React relay pendingVariables always null after setVariables

When i try to set a variable, the component updates only after fetch is complete. 当我尝试设置变量时,组件仅在获取完成后更新。 Here is an exapmle code. 这是一个exapmle代码。

class RelayExample extends React.Component {
  setVars() {
    this.props.relay.setVariables({id: '2'});
  };
  render() {
    console.log(this.props.relay.pendingVariables);
    return (
      <div>
        <button onClick={this.setVars.bind(this)}>set variable</button>
      </div>
    );
  }
}

RelayExample = Relay.createContainer(RelayExample, {
  initialVariables: {
    id: '1'
  },
  fragments: {
    userStore: ()=> {
      return Relay.QL`
      fragment on userStore {
         user(id:$id){
          email
         }
      }
      `;
    }
  }
});

ReactDOM.render(
  <Relay.RootContainer Component={RelayExample} route={new TestQuery()}/>,
    document.getElementById('app')
);

When i press the button i get this result in console: 当我按下按钮时,我在控制台中得到这个结果: 在此输入图像描述

Even if i use forceUpdate right after the setVariables function, i get similar result. 即使我在setVariables函数之后立即使用forceUpdate,我也得到类似的结果。

setVars() {
        this.props.relay.setVariables({id: '2'});
        this.forceUpdate();
};

在此输入图像描述

I believe this is intended behaviour. 我相信这是预期的行为。

pendingVariables contains the set of variables that are being used to fetch the new props, ie when this.props.relay.setVariables() or this.props.relay.forceFetch() are called and the corresponding request is in flight. pendingVariables包含用于获取新props的变量集,即当调用this.props.relay.setVariables()或this.props.relay.forceFetch()并且相应的请求在飞行中时。

If no request is in flight pendingVariables is null. 如果没有请求正在等待pendingVariables为null。

If you console log right after you set your variables you should see them in pendingVariables . 如果在设置变量后立即控制日志,则应在pendingVariables看到它们。

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

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