javascript - 在 setVariables 之后 React 继电器挂起的变量总是为 null

标签 javascript reactjs graphql relayjs

当我尝试设置变量时,组件仅在获取完成后更新。这是一个示例代码。

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')
);

当我按下按钮时,我在控制台中得到以下结果: enter image description here

即使我在 setVariables 函数之后立即使用forceUpdate,我也会得到类似的结果。

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

enter image description here

最佳答案

我相信这是有意的行为。

pendingVariables contains the set of variables that are being used to fetch the new props, i.e. when this.props.relay.setVariables() or this.props.relay.forceFetch() are called and the corresponding request is in flight.

If no request is in flight pendingVariables is null.

如果您在设置变量后立即控制台日志,您应该在 pendingVariables 中看到它们。

关于javascript - 在 setVariables 之后 React 继电器挂起的变量总是为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40717117/

相关文章:

javascript - _Layout.cshtml 中引用的 JS 文件/函数不适用于其他 View

javascript - 如何从文本区域输入创建 $_POST 数组

javascript - 如何在 THREE.SphereGeometry 上获取纹理图像的触摸位置

javascript - d3 更改 x 轴时间仅勾选整年和整月

javascript - Firebase + React Native - 获取每个文档 ID

javascript - GraphQL 查询错误它说未知类型 "ContentfulSizes"

javascript - 组件 this.props 日志记录在控制台中未定义,但显示在 React 开发人员工具中

javascript - React-Router 无法读取未定义的属性字符串

Github GraphQL API v.4.0 游标错误

javascript - s 时改变子组件的 prop