gpt4 book ai didi

javascript - 当 React 已经渲染时无法刷新更新

转载 作者:行者123 更新时间:2023-12-03 13:02:46 25 4
gpt4 key购买 nike

我尝试在 API 返回错误时显示警报。对于警报窗口,我使用 sweetalert2。在我的渲染方法中,我正在检查错误消息是否包含内容。如果它包含错误消息,我想向用户显示警报。

当我提交表单时,我会进行 API 调用。如果返回错误,reducer 会更改存储(状态)并再次呈现页面。

自从我添加下面的行后,我不断收到错误:

{saveLabelFetchError && this.toggleAlertFailure(saveLabelFetchError)}

错误:

index.js:1375 Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering.

我的组件:

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);

我想知道为什么我的控制台中不断收到此错误消息。

最佳答案

{saveLabelFetchError && this.toggleAlertFailure(saveLabelFetchError)}

您正在尝试在渲染周期之前(即在组件安装之前)更新 dom。因此,您会收到错误。

理想情况下,您应该避免将任何直接改变 dom 的库(而不是通过 react API)与 React lib 一起使用。您可以阅读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);


您还可以查看react wrapper for sweetalert2

关于javascript - 当 React 已经渲染时无法刷新更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58040892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com