[英]ReactJS | Cannot update during an existing state transition
I am trying to add Delete Functionality, to my React Application. 我正在尝试将删除功能添加到我的React应用程序中。 So, I created a Delete Model Component. 因此,我创建了一个删除模型组件。 And I am using it in my main page. 我在主页上使用它。
Main-Page Component: 主页组件:
import IUser from '../../dto/IUser';
import DeleteUser from '../../components/DeleteUser';
import { listUsers, getUser, deleteUser } from '../../config/service';
interface UserDetailsProps extends RouteComponentProps<RouteUserInfo> {
notify(options: object): any;
actualValue: string;
callBack: any;
label: string;
}
interface RouteUserInfo {
username: string;
}
export interface IState {
errorMessage: LensesHttpResponseObj | null;
isUserDeleteModalOpen: boolean;
isLoading: boolean;
user: IUser | null;
}
const UserToolTip = (props: any): JSX.Element => (
<LensesTooltip id="isActive" place="right" {...props} />
);
export class UserDetailsPage extends Component<UserDetailsProps, IState> {
hasBeenMounted = false;
state: IState = {
isUserDeleteModalOpen: false,
errorMessage: null,
isLoading: false,
user: null
};
componentDidMount(): any {
this.hasBeenMounted = true;
this.onFetchData();
}
componentWillUnmount(): void {
this.hasBeenMounted = false;
}
getUserUsername = (): string => {
const { match } = this.props;
return match.params.username;
};
onFetchData = () => {
this.setState({
isLoading: true
});
return this.onFetchUser();
};
onFetchUser = () =>
getUser(this.getUserUsername())
.then(username => {
if (this.hasBeenMounted && typeof username !== 'undefined') {
this.setState({
isLoading: false,
user: username.data
});
}
})
.catch((errorResponse: HttpResponseObj | null = null) => {
if (this.hasBeenMounted) {
this.setState({
isLoading: false,
user: null,
errorMessage: errorResponse
});
}
});
openUserDeleteModal = () => {
this.setState(prevState => ({
...prevState,
isUserDeleteModalOpen: true
}));
};
closeUserDeleteModal = () => {
this.setState(prevState => ({
...prevState,
isUserDeleteModalOpen: false
}));
};
// Dropdown Render Method:
<Item onClick={this.openUserDeleteModal()}> // The error appears when I add the onClik
<Icon icon="trash-o" className=" pl-0 py-2 col-1" />
<span className="col pr-0 mr-0">Delete User</span>
</Item>
Of course I call the dropdown render Method inside the main render()
, along with the render method for the Delete Component: 当然,我会在主render()
内部调用下拉render方法,以及Delete Component的render方法:
renderUserDeleteModal = (): JSX.Element | null | void => {
const { isUserDeleteModalOpen, user } = this.state;
if (!user || !user.username) {
return null;
}
return (
<DeleteUser
isModalOpen={isUserDeleteModalOpen}
user={user}
onSuccess={this.closeDeleteModalSuccess}
onCloseModal={this.closeUserDeleteModal}
/>
);
};
But I get this ERROR: warning: Cannot update during an existing state transition (such as within
render ). Render methods should be a pure function of props and state.
但是我收到此错误: warning: Cannot update during an existing state transition (such as within
render warning: Cannot update during an existing state transition (such as within
). Render methods should be a pure function of props and state.
warning: Cannot update during an existing state transition (such as within
). Render methods should be a pure function of props and state.
). Render methods should be a pure function of props and state.
I am not sure what am I doing wrong here. 我不确定我在做什么错。 To me, it seems legit. 在我看来,这是合法的。 Can you explain what am I doing wrong. 你能解释我在做什么错。 Thank you!! 谢谢!!
you are making call to openUserDeleteModal onClick={this.openUserDeleteModal()} which is causing update of state while rendering the component try the following : 您正在调用openUserDeleteModal onClick = {this.openUserDeleteModal()},这会在渲染组件时引起状态更新,请尝试以下操作:
<Item onClick={this.openUserDeleteModal}>
onClik
<Icon icon="trash-o" className=" pl-0 py-2 col-1" />
<span className="col pr-0 mr-0">Delete User</span>
</Item>
You need not invoke the callback to your onClick
as that will end up being immediately called upon render. 您无需调用onClick
的回调,因为最终会在渲染时立即被调用。
Remove the parenthesis following onClick={openUserDelete ()
}. 在onClick = {openUserDelete ()
}之后删除括号。
Your openUserDelete
is being called straight away (upon render) and changes the state Object. 您的openUserDelete
被立即调用(在渲染时)并更改状态Object。
changing the state causes a re-render and you can imagine how this would get out of hand... render > change > render > change...etc
更改状态会导致重新渲染,您可以想象这将如何失控... render > change > render > change...etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.