gpt4 book ai didi

javascript - 如何在 React 中的卸载组件中设置 State

转载 作者:行者123 更新时间:2023-11-28 14:32:08 25 4
gpt4 key购买 nike

我们如何在参数内的 componentDidMount Hook 中设置 State?

我的问题是React通知的警告:

Warning: Can't call setState (or forceUpdate) 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 DiscoverPage (created by Route)

在我的 DiscoverPage 组件中:

import React, { Component } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import TvShows from './TvShows';
import Movies from './Movies';
import MovieDetail from "../MoviePage/MovieDetail";
import TvDetail from "../TvShowPage/TvDetail";

class DiscoverPage extends Component {

constructor(props) {
super(props);
this.state = {
data: {}
}
}

getDataItem = (data) => {
this.setState({ data: data });
};

render() {
return (
<Switch>
<Route exact path="/discover/movie" render={props => <Movies data={this.getDataItem} {...props} />} />
<Route path="/discover/tv" render={props => <TvShows data={this.getDataItem} {...props} />} />
<Route path="/movie/:movie" render={props => <MovieDetail data={this.state.data} {...props} />} />
<Route path="/tv/:tv" render={props => <TvDetail data={this.state.data} {...props} />} />
<Redirect to="/discover/movie" />
</Switch>
);
}
}

export default DiscoverPage;

在子组件中(本例是 Movies 组件):

import React, { Component } from 'react';
import requestApi from '../api';
import MovieList from "../MoviePage/MovieList";

class Movies extends Component {

constructor(props) {
super(props);
this.state = {
data: {
page: 1,
results: []
}
}
}

componentDidMount() {
requestApi.fetchData('discover', 'movie').then(response => {
this.setState({ data: response.data, isLoading: false });
});
}

getMoviebyId = (id) => {
requestApi.fetchDataById('movie', id).then(response => {
this.props.data(response.data);
});
};

nextPage = (e) => {
const page = this.state.data.page + 1;
requestApi.fetchDataPaginate('discover', 'movie', page).then(response => {
this.setState({ data: response.data });
});
};
prevPaginate = (e) => {
const page = this.state.data.page - 1;
requestApi.fetchDataPaginate('discover', 'movie', page).then(response => {
this.setState({ data: response.data });
});
};

render() {
return (
<div className="container">
<div className="ss_media">
<h2 className="title">Discover New Movies & TV Shows</h2>
<MovieList
movie={this.getMoviebyId}
routeProps={this.props}
prevPaginate={this.prevPaginate}
nextPaginate={this.nextPage}
moviesList={this.state.data} />
</div>
</div>
);
}
}

export default Movies;

最佳答案

当路由更改以及调用 getDataItem 方法时,您的 DiscoverPage 组件将卸载。这就是 react 抛出错误的原因。

我建议使用react-router组件方法,而不是渲染并将数据移动到存储中,然后可以由任何组件访问。请参阅react-router render methods

示例:

<Route path="/user/:username" component={User}/>

关于javascript - 如何在 React 中的卸载组件中设置 State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51039323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com