Reactjs Can';是否在未安装的组件上执行React状态更新?
我正在componentDidMount中获取数据并更新状态,出现了著名的警告: 无法对未安装的组件执行React状态更新。这是 无操作,但表示应用程序内存泄漏。要解决, 取消中的所有订阅和异步任务 componentWillUnmount方法Reactjs Can';是否在未安装的组件上执行React状态更新?,reactjs,Reactjs,我正在componentDidMount中获取数据并更新状态,出现了著名的警告: 无法对未安装的组件执行React状态更新。这是 无操作,但表示应用程序内存泄漏。要解决, 取消中的所有订阅和异步任务 componentWillUnmount方法 状态={ post:null } render(){ console.log(this.props.posted) 如果(此.props.post){ 返回( {this.state.post.postTitle} {this.state.post.po
状态={
post:null
}
render(){
console.log(this.props.posted)
如果(此.props.post){
返回(
{this.state.post.postTitle}
{this.state.post.postBody}
);
}否则{
返回
}
}
componentDidMount(){
这个。_isMounted=true;
axios
.get('https://jsonplaceholder.typicode.com/posts/“++this.props.posted)
。然后((响应)=>{
让post={
id:response.data.id,
postTitle:response.data.title,
postBody:response.data.body
}
this.setState((prevState,props)=>{
console.log('post',post)
console.log('prevState',prevState)
console.log('props',props)
})
})
.catch((e)=>{
console.log('error',e)
})
}
}
导致此警告的原因是什么?获取数据和更新状态的最佳方法是什么?您的问题与以下问题相同 如何开发组件,可以进入循环并无限执行渲染。为了防止这种情况发生,请再添加一个状态和控件,以便只发出一次请求,或者添加那些需要但“注意”的状态和控件 此外,您的代码需要一些更改,您可以使用状态来验证是否显示某些内容,并且在发出请求后,再次设置状态。通过一些更改,您的代码可以如下所示:
state = {
loadingData: true,
post: null
}
render() {
if (!this.state.loadingData) {
return (
<div>
<h3> {this.state.post.postTitle} </h3>
<p> {this.state.post.postBody} </p>
</div>
);
} else {
return <Redirect to="/blog" />
}
}
componentDidMount() {
this._isMounted = true;
if(this.state.loadingData)
axios
.get('https://jsonplaceholder.typicode.com/posts/' + + this.props.postId)
.then((response) => {
this.setState({
loadingData: false,
post: {
id: response.data.id,
postTitle: response.data.title,
postBody: response.data.body
}
})
})
.catch((e) => {
console.log('error', e)
})
}
状态={
加载数据:true,
post:null
}
render(){
如果(!this.state.loadingData){
返回(
{this.state.post.postTitle}
{this.state.post.postBody}
);
}否则{
返回
}
}
componentDidMount(){
这个。_isMounted=true;
if(this.state.loadingData)
axios
.get('https://jsonplaceholder.typicode.com/posts/“++this.props.posted)
。然后((响应)=>{
这是我的国家({
加载数据:false,
职位:{
id:response.data.id,
postTitle:response.data.title,
postBody:response.data.body
}
})
})
.catch((e)=>{
console.log('error',e)
})
}
我希望它有用。
关于仅在状态中创建标志:
isMounted:false
。在componentDidMount中设置状态isMounted=true
和在componentWillUnmount中设置状态isMounted=false
在需要使用setState函数的异步函数中,如果(this.state.isMounted){…setState action}在Ajax请求通过axios完成之前,组件可能会再次卸载。
state = {
loadingData: true,
post: null
}
render() {
if (!this.state.loadingData) {
return (
<div>
<h3> {this.state.post.postTitle} </h3>
<p> {this.state.post.postBody} </p>
</div>
);
} else {
return <Redirect to="/blog" />
}
}
componentDidMount() {
this._isMounted = true;
if(this.state.loadingData)
axios
.get('https://jsonplaceholder.typicode.com/posts/' + + this.props.postId)
.then((response) => {
this.setState({
loadingData: false,
post: {
id: response.data.id,
postTitle: response.data.title,
postBody: response.data.body
}
})
})
.catch((e) => {
console.log('error', e)
})
}