如果不满足条件,如何在不重复的情况下执行内部函数?

How to execute a function inside if without being repetitive if the condition is not met?

这是中继突变的代码,我必须重新加载,这样存储才能与数据库同步,因为出于某种原因,如果文本与之前添加的文本相同,中继存储会抛出错误 flattenChildren.. .

  _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 是多余的。你怎么看?我将不胜感激。

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

_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();
};