我是React Native的新手,我在这个项目中遇到ReferenceError错误, 当我进入一个显示所有用户喜欢的电影的页面时,就会看到:
ReferenceError: ReferenceError: Can't find variable: loadMoviesLikedDetails
这是组件代码的一部分:
class LikedScreen extends Component {
constructor(props) {
super(props);
this.state = {
moviesLikedDetails: []
};
}
componentDidMount() {
loadMoviesLikedDetails(); <-- SEEMS LIKE THIS CREATES THE ERROR
}
loadMoviesLikedDetails() {
this.props.likedFilms.forEach(async movie => {
const dataDetail = axios.get(
`https://api.themoviedb.org/3/movie/${movie}?api_key=${API_KEY}&language=fr`
);
this.setState({
moviesLikedDetails: [...this.state.moviesLikedDetails, dataDetail.data]
});
});
}
render() {
return (
<View style={styles.container}>
{this.state.moviesLikedDetails.map(movie => (
<View style={{ display: "flex", flex: "wrap", width: "25%" }}>
{movie.id}
</View>
))}
</View>
);
}
}
似乎错误来自于componentDidMount中函数的执行...
答案 0 :(得分:1)
您在this.loadMoviesLikedDetails()
中缺少componentDidUpdate
此外,您可以这样定义状态,而不是constructor
state = {
moviesLikedDetails: []
}
您还可以选中hooks