我收到以下錯誤時,無法更新:警告:的setState(...):現有的狀態轉換
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
我的組件:
import React from 'react';
import { Redirect } from 'react-router'
import Notifications from 'react-notification-system-redux';
constructor(props) {
super(props);
this.state = {
invite_token: this.props.match.params.token,
FormSubmitSucceeded: false,
inviteRequestSubmitSucceeded: false
};
}
....
inviteAlreadyUsed() {
const notificationOpts = {
message: 'Invitation already used!',
};
this.props.createNotificationSuccess(notificationOpts);
}
render() {
const { invite } = this.props;
if (invite && invite.status === "completed") {
this.inviteAlreadyUsed();
return <Redirect to={{ pathname: '/' }}/>;
}
...
如何避免任何建議這個警告?這不是你將如何處理重定向?
我可以看到你的構造函數嗎? –
@HanaAlaydrus added – AnApprentice
您的示例沒有顯示您設置狀態的位置......警告提示您在構造函數或渲染方法中調用它。你必須找到它發生的地方,然後重構'''setState''' – archae0pteryx