我试图在API返回错误时显示警报。对于警报窗口,我正在使用sweetalert2。在我的渲染方法中,我正在检查错误消息是否包含内容。如果它包含错误消息,我想向用户显示警报。
提交表单时,我会进行API调用。如果返回错误,则reducer更改存储(状态),并再次呈现页面。
由于我添加了以下行,所以我不断收到错误消息:
{saveLabelFetchError && this.toggleAlertFailure(saveLabelFetchError)}
错误:
index.js:1375警告:unable_flushDiscreteUpdates:无法刷新更新 当React已经渲染的时候。
我的组件
import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { saveLabelValueRequest } from "../../actions/labels";
import Swal from "sweetalert2";
import "./styles.css";
import Button from "@kof/button";
export class NewLabels extends Component {
state = {
labelInput: ""
};
inputChangedhandler = e => {
this.setState({ labelInput: e.target.value });
};
toggleAlertFailure = message => {
Swal.fire({
type: "error",
title: "Oops...",
text: message
});
};
saveLabel = event => {
event.persist();
event.preventDefault();
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, save it."
}).then(result => {
if (result.value) {
const labelKeyUuid = this.props.labelKey.uuid;
const labels = event.target.elements.labels.value;
this.props.saveLabelValue(labelKeyUuid, labels);
}
});
};
render() {
const { load, saveLabelFetchError } = this.props;
return (
<div>
<form onSubmit={this.saveLabel}>
<textarea onChange={this.inputChangedhandler}></textarea>
<textarea></textarea>
<Button onClick={() => load(this.state.labelInput)}>Preview</Button>
<Button type="submit">Save</Button>
</form>
{saveLabelFetchError && this.toggleAlertFailure(saveLabelFetchError)}
</div>
);
}
}
NewLabels.propTypes = {
saveLabelFetchError: PropTypes.string,
isFetching: PropTypes.bool,
labelKey: PropTypes.object,
saveLabelValue: PropTypes.func
};
NewLabels.defaultProps = {
saveLabelFetchError: "",
labelKey: {},
isFetching: false,
saveLabelValue: () => {}
};
export default connect(
state => ({
saveLabelFetchError: state.labelsStore.saveLabelError,
isFetching: state.labelsStore.isFetching,
labelKey: state.labelsStore.labelKey
}),
dispatch =>
bindActionCreators(
{
saveLabelValue: saveLabelValueRequest
},
dispatch
)
)(NewLabels);
我想知道为什么我不断在控制台中收到此错误消息。
答案 0 :(得分:4)
{saveLabelFetchError && this.toggleAlertFailure(saveLabelFetchError)}
您正在尝试在渲染周期之前(即在安装组件之前)更新dom。因此,您会得到一个错误。
理想情况下,您应该避免使用任何将react lib直接更改dom(而不是通过react
API进行更改)的库。您可以从here
解决方案是检查props值是否有变化,如果有,则显示错误弹出窗口。还要确保我们在react的渲染周期中不改变dom。
import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { saveLabelValueRequest } from "../../actions/labels";
import Swal from "sweetalert2";
import "./styles.css";
import Button from "@kof/button";
export class NewLabels extends Component {
state = {
labelInput: "",
prevSaveLabelFetchError: ""
};
static getDerivedStateFromProps(props, state) {
if (props.saveLabelFetchError !== state.prevSaveLabelFetchError) {
this.toggleAlertFailure(props.saveLabelFetchError);
return {
prevSaveLabelFetchError: props.saveLabelFetchError
};
}
return null;
}
inputChangedhandler = e => {
this.setState({ labelInput: e.target.value });
};
toggleAlertFailure = message => {
Swal.fire({
type: "error",
title: "Oops...",
text: message
});
};
saveLabel = event => {
event.persist();
event.preventDefault();
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, save it."
}).then(result => {
if (result.value) {
const labelKeyUuid = this.props.labelKey.uuid;
const labels = event.target.elements.labels.value;
this.props.saveLabelValue(labelKeyUuid, labels);
}
});
};
render() {
const { load } = this.props;
return (
<div>
<form onSubmit={this.saveLabel}>
<textarea onChange={this.inputChangedhandler}></textarea>
<textarea></textarea>
<Button onClick={() => load(this.state.labelInput)}>Preview</Button>
<Button type="submit">Save</Button>
</form>
</div>
);
}
}
NewLabels.propTypes = {
saveLabelFetchError: PropTypes.string,
isFetching: PropTypes.bool,
labelKey: PropTypes.object,
saveLabelValue: PropTypes.func
};
NewLabels.defaultProps = {
saveLabelFetchError: "",
labelKey: {},
isFetching: false,
saveLabelValue: () => {}
};
export default connect(
state => ({
saveLabelFetchError: state.labelsStore.saveLabelError,
isFetching: state.labelsStore.isFetching,
labelKey: state.labelsStore.labelKey
}),
dispatch =>
bindActionCreators(
{
saveLabelValue: saveLabelValueRequest
},
dispatch
)
)(NewLabels);
答案 1 :(得分:0)
试试这个代码片段:
const [state, setState] = useState(true)
const renderFunc = () => {
...
setState(false)
}
state && renderFunc()