我有一个Submit form方法,该方法看起来很正常,但是会发出警告,我想知道为什么? 警告是“ warning.js?6327:33警告:setState(...):只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是空操作。请“
constructor(props) {
super(props);
this.handleForm = this.handleForm.bind(this);
this.handleState = this.handleState.bind(this);
this.submitForm = this.submitForm.bind(this);
this.submitButton = null;
let form = null;
if (props.node !== null) {
form = { ...props.node };
} else {
form = {
name: null,
description: null,
enabled: null,
cost: null,
thumbnail: null,
image: null,
grants: []
};
}
this.state = {
isLoading: false,
success: null,
form: form,
errorMessages: null,
invalid: {
name: false,
description: false,
enabled: false,
cost: false,
thumbnail: false,
image: false,
grants: false
}
};
}
async handleForm(event) {
event.preventDefault();
if (!this.refs.form || this.refs.form.checkValidity()) {
try {
let result = null;
let serviceId;
if (this.state.form.id) {
result = await editMutation(
this.state.form.id,
this.state.form.name,
this.state.form.description,
this.state.form.enabled,
this.state.form.cost,
this.state.form.thumbnail,
this.state.form.image,
this.state.form.grants
);
serviceId = result.serviceEditMutation.service.id;
} else {
result = await addMutation(
this.state.form.name,
this.state.form.description,
this.state.form.enabled,
this.state.form.cost,
this.state.form.thumbnail,
this.state.form.image,
this.state.form.grants
);
serviceId = result.serviceAddMutation.service.id;
}
serviceIdAction.serviceAction(serviceId);
this.setState({ success: true });
} catch (e) {
this.setState({ success: false, errorMessages: e });
}
}
}