如果不满足条件,如果不重复,如何执行内部函数?

时间:2017-05-11 20:53:29

标签: javascript reactjs ecmascript-6 relayjs

这是继电器变异的代码,我必须重新加载,因此存储将与数据库同步,因为由于某种原因,如果文本与先前添加的文本中继存储相同,则抛出错误flattenChildren ... < / p>

  _handleTextInputSave = (text) => {
    if(checkIfTextAlreadyExists(text) && window.confirm("Todo already exists! Please confirm to proceed.")) {
      AddTodoMutation.commit(
        this.props.relay.environment,
        text,
        this.props.viewer,
      );
     location.reload();
    }
    AddTodoMutation.commit(
      this.props.relay.environment,
      text,
      this.props.viewer,
    );
  };

我无法想到其他方式,因为如果文本已经存在我必须重新加载但不知何故我觉得AddTodoMutation.commit是多余的。你怎么看?我很感激建议。

1 个答案:

答案 0 :(得分:2)

看起来你总是想提交并且只是有条件地重新加载。所以写下:

_handleTextInputSave = (text) => {
  const exists = checkIfTextAlreadyExists(text) && window.confirm("Todo already exists! Please confirm to proceed.");
  AddTodoMutation.commit(
    this.props.relay.environment,
    text,
    this.props.viewer,
  );
  if (exists)
    location.reload();
};