简体   繁体   English

ReferenceError:找不到变量:loadMoviesLikedDetails(React Native)

[英]ReferenceError: Can't find variable: loadMoviesLikedDetails (React Native)

I'm new to React Native and I have this project where I have a ReferenceError error, When I get into a page that display all movies the user liked, I have this:我是 React Native 的新手,我有这个项目,我有一个 ReferenceError 错误,当我进入一个显示用户喜欢的所有电影的页面时,我有这个:

ReferenceError: ReferenceError: Can't find variable: loadMoviesLikedDetails

Here's a part of the code of the component:这是组件的一部分代码:

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>
      );
  }
}

It looks like the error comes from the execution of the function in the componentDidMount...看起来错误来自 componentDidMount 中 function 的执行...

You are missing a this.loadMoviesLikedDetails() in the componentDidUpdate您在componentDidUpdate中缺少this.loadMoviesLikedDetails()

Also, you can define your state this way, rather than a constructor此外,您可以通过这种方式定义 state,而不是constructor

  state = {
    moviesLikedDetails: []
  }

You can also check hooks你也可以检查钩子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReferenceError: 找不到变量: False - React Native (Hookstate) - ReferenceError: Can't find variable: False - React Native (Hookstate) REACT-NATIVE - abcjs - ReferenceError:找不到变量:元素 - REACT-NATIVE - abcjs - ReferenceError: Can't find variable: Element 安装 React 时出现 React Native 错误(ReferenceError:找不到变量:React)? - React Native Error (ReferenceError: Can't find variable: React) when React is installed? React Native Formik 与 react redux 一起使用(参考错误:找不到变量:标题) - React Native Formik use with react redux (ReferenceError: Can't Find Variable: title) React Native:ReferenceError:找不到变量:require(生成的包中的第1行) - React Native: ReferenceError: Can't find variable: require (line 1 in the generated bundle) 获取ReferenceError:找不到变量:是否需要webpack? - Getting ReferenceError: Can't find variable: require react webpack? React Navigation ReferenceError:找不到变量:isSelectionModeEnabled - React Navigation ReferenceError: Can't find variable: isSelectionModeEnabled TypeScript React Babel ReferenceError:找不到变量:regeneratorRuntime - TypeScript React Babel ReferenceError: Can't find variable: regeneratorRuntime React Native:找不到变量:require - React Native: Can't find variable: require 在 React Native 中找不到变量 - Can't find variable in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM