问:为什么在组件不再由其父组件呈现之后抛出此警告?我是不是遗漏了一些需要做的事情来卸载这个组件,而不仅仅是过滤作为道具传递给组件层次结构的存储状态?
我已经看到了很多这样的场景,但是解决方案通常涉及从组件中取消redux存储的订阅;然而,这个组件并不连接到存储,只是顶级容器。
remove操作只需过滤存储状态,以删除负责当前组件的数组元素。refresh操作目前只是子组件中UI动画事件的模拟。Feed操作后删除refresh组件时才会引发警告。警告:setState(.):只能更新安装或安装组件。这通常意味着在未挂载的组件上调用setState()。这是禁止行动。请检查Feed组件的代码。
// @flow
// Feed.js
import React, { Component } from 'react'
import type { FeedType, FeedState } from '../../utils/types'
import { remove, refresh } from '../../actions/redux-actions'
import RssEventList from '../containers/RssEventList'
const cardColors: Array<string> = ['red', 'orange', 'olive', 'green', 'blue', 'yellow']
export default class Feed extends Component {
props: FeedType
state: FeedState
constructor(props: *) {
super(props)
this.state = {
reloading: false
}
}
refresh() {
this.setState({ reloading: true })
setInterval(() => this.setState({ reloading: false }), 4000)
this.props.dispatch(refresh(this.props.link))
}
remove() {
this.props.dispatch(remove(this.props.link))
}
render() {
const color: string = cardColors[Math.floor(Math.random() * cardColors.length)]
return (
<div className={`ui ${color} card`}>
<div className="content">
<div className="ui header">
{this.props.title}
<a className="source link" href={this.props.link} target="_blank">
<i className="linkify right floated icon"></i>
</a>
</div>
<div className="meta">
{this.props.description}
</div>
</div>
<div className="content">
<RssEventList reloading={this.state.reloading} events={this.props.feed} />
</div>
<div className="extra content">
<span className="left floated" onClick={() => this.refresh()}>
<i className="refresh icon"></i>
Refresh
</span>
<span className="right floated" onClick={() => this.remove()}>
<i className="cancel icon"></i>
Remove
</span>
</div>
</div>
)
}
}如果有帮助,下面是组件层次结构的图表:
App (connected to store)
|- Header
|- FilterBar
|- FeedList
|- Feed
|- RssEventList
|- RssEvent
|- AddCard发布于 2017-03-29 15:33:44
问题是,当组件卸载时,您没有将您的间隔存储在组件上以删除它。因此,即使在卸载组件之后,仍将继续调用该间隔。您需要用clearInterval()删除它。
export default class Feed extends Component {
refresh() {
this.myInterval = setInterval(() => this.setState({ reloading: false }), 4000)
}
componentWillUnmount() {
clearInterval(this.myInterval);
}
}https://stackoverflow.com/questions/43097815
复制相似问题