简体   繁体   English

React 组件仅检索一次道具,刷新时未定义

[英]React component retrieves props just once, goes undefined when refreshed

I'm creating a simple movie app with moviedb.我正在用moviedb 创建一个简单的电影应用程序。 I have successfully retrieved the most popular 20 movies and put them in the app state:我已成功检索到最受欢迎的 20 部电影并将它们放入应用 state 中:

  constructor(props) {
    super(props);
    this.state = {
      movieInfo: [],
    }
  }

  componentDidMount() {
    this.getMovies();
  }

  getMovies = async () => {
    await axios.get('https://api.themoviedb.org/3/movie/popular?api_key=94d4ad026c5009bdaf4aecb8989dfa07')
    .then(res => this.setState({ movieInfo: res.data.results }))
  }

I know the array was retrieved correctly because when I look at the React components in Chrome dev tools I see what I want:我知道数组已正确检索,因为当我查看 Chrome 开发工具中的 React 组件时,我看到了我想要的:

Screen cap of App state App state 的屏幕截图

Then in the render part of the App I want to pass the first element in the array to a component called Movie, which will then display some info about the movie:然后在应用程序的渲染部分,我想将数组中的第一个元素传递给一个名为 Movie 的组件,然后该组件将显示有关电影的一些信息:

    return (
      <div>
        <Movie movie={this.state.movieInfo[0]} />
      </div>
    );
  }

I know the movie component is getting this info correctly because I see the object representing the first movie in the Movie component props:我知道电影组件正确获取此信息,因为我看到 object 代表电影组件道具中的第一部电影:

Movie component props电影组件道具

My Movie function looks like this:我的电影 function 看起来像这样:

    return (
        <div>
            <h1>{props.movie.original_title}</h1>
            <p>{props.movie.overview}</p>
        </div>
    )
}

The first time I compile it this works and I see the info I want:我第一次编译它就可以了,我看到了我想要的信息:

Rendered App with Movie component带有电影组件的渲染应用程序

But incredibly, when I refresh the page I see the error message但令人难以置信的是,当我刷新页面时,我看到了错误消息

TypeError: Cannot read properties of undefined (reading 'original_title')

How is it possible that the App will correctly pass on the info to Movie and will display it correctly once, but as soon as I refresh the page somehow it's undefined?应用程序怎么可能正确地将信息传递给电影并正确显示一次,但是一旦我以某种方式刷新页面,它就不确定了?

Thanks in advance for the help, JD提前感谢您的帮助,JD

You may need to also use componentDidUpdate() with componentDidMount():您可能还需要将 componentDidUpdate() 与 componentDidMount() 一起使用:

componentDidMount() {
    this.getMovies();
}
componentDidUpdate() {
    this.getMovies();
}

I assume that you don't get an error when you are developing and changing code.我假设您在开发和更改代码时不会出错。 When you save your code Hot reloading only changes parts that changed.当您保存代码时,热重载只会更改已更改的部分。

This means that if your app loads data and populates this.state.movieInfo with an array from BE, and your save you code, it Hot reloads and you get new data.这意味着如果您的应用程序加载数据并使用来自 BE 的数组填充this.state.movieInfo ,并且您保存代码,它会热重新加载并获得新数据。 So, this.state.movieInfo[0] is always filled with data.所以, this.state.movieInfo[0]总是填充数据。

When you refresh your app, it just resets to an empty array as you put it there in the constructor .当你刷新你的应用程序时,它只是重置为一个空数组,因为你把它放在constructor中。

Solution is to always check if there is that first element in array before rendering the Movie component:解决方案是在渲染Movie组件之前始终检查数组中是否存在第一个元素:

return (
  <div>
    {this.state.movieInfo[0] ? <Movie movie={this.state.movieInfo[0]} /> : null}
  </div>
);

when the page reloads init state is an empty array.当页面重新加载时,init state 是一个空数组。 you have to check the array has an item to render or not.您必须检查数组是否有要渲染的项目。

return (
  <div>
    {this.state.movieInfo && this.state.movieInfo.length>0 && (<Movie 
    movie={this.state.movieInfo[0]} />)}
    
  </div>
);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM