Reactjs Can';当过滤器元件处于redux时,不要对未安装的组件执行React状态更新
我在添加案例时收到警告:Reactjs Can';当过滤器元件处于redux时,不要对未安装的组件执行React状态更新,reactjs,redux,Reactjs,Redux,我在添加案例时收到警告:DELETE\u SECRETin-storeredux const mySecretsReducer = (state = initialState, action) => { switch (action.type) { case GET_MY_SECRETS: return { items: action.payload } case DEL
DELETE\u SECRET
in-storeredux
const mySecretsReducer = (state = initialState, action) => {
switch (action.type) {
case GET_MY_SECRETS:
return {
items: action.payload
}
case DELETE_SECRET:
return {
items: state.items.filter(e => e.id !== action.payload.id)
}
default:
return state;
}
}
删除元素后,我收到警告:
index.js:1 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
in index (created by ConnectFunction)
in ConnectFunction (at TopSecret/index.js:12)
in div (at Top/index.js:11)
in div (at Top/index.js:8)
in index (at One/index.js:21)
in div (at One/index.js:20)
这是我删除的组件中的func setState
deleteMySecret = () => {
let params = {
id: this.props.id
}
this.props.deleteMySecret(params)
.then(res => {
console.log(res.response);
this.setState({ showPopConfirm: !this.state.showPopConfirm });
}
)
.catch(res => {
console.log(res.response);
this.setState({ showPopConfirm: !this.state.showPopConfirm })
}
)
}
changeStatePopConfirm = () => {
this.setState({ showPopConfirm: !this.state.showPopConfirm })
}
如何修复它。您可能正在更新组件中的状态(通过
设置状态
),该组件以前呈现了您从状态中删除的项。@Scarysize yes,以及我如何修复它。不要在安装组件时通过检查异步任务解决时的状态来设置状态。您发布的代码与您所遇到的错误无关(我看不到在异步解析后调用任何状态设置程序函数),因此无法提供更多建议。@而且,如果是修复,警告仍应修复,这将有助于您了解未来的错误谢谢大家,我从这里得到了解决方案:您可能正在更新组件中的状态(通过设置状态
),该组件以前呈现了您从状态中删除的项。@请确定是,以及如何修复它。不要通过检查异步任务是否解决了该问题来设置组件装入时的状态。您发布的代码与您遇到的错误无关(我看不到在异步解析后调用任何状态设置程序函数),因此无法提供更多建议。@而且,如果是修复,警告仍应修复,这将有助于您了解未来的错误谢谢大家,我从这里得到了解决方法: