作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们如何在参数内的 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/
我是一名优秀的程序员,十分优秀!