[英]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();
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.