admin管理员组文章数量:1426118
I'm trying to render a list of movies on a page using OMDbApi but nothing renders on the page yet im not getting any error in the console so im confused. the react dev tools shows contents in the array when o check the state yet nothing is still rendered on the page what am i doing wrong?
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
movies: [] //default state
}
};
ponentWillMount(){
let movieApi = '/?apikey=[apikey]&s=harry'
fetch(movieApi)
.then(data => data.json())
.then(movies => this.setState({movies}))
}
// <li>
// <img src="<%= movie['Poster'] %>">
// <b><%= movie['Title'] %></b> -
// <%= movie['Year'] %>
// </li>
render() {
let views = <div>Loading...</div>
const {movies} = this.state;
if(movies && movies.length > 0) {
views = movies.Search.map(m => (
<li key={m}>
<b>{m.Title}</b> - <strong>{m.Year}</strong>
</li>
))
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
{views}
</div>
);
}
}
export default App;
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>
I'm trying to render a list of movies on a page using OMDbApi but nothing renders on the page yet im not getting any error in the console so im confused. the react dev tools shows contents in the array when o check the state yet nothing is still rendered on the page what am i doing wrong?
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
movies: [] //default state
}
};
ponentWillMount(){
let movieApi = 'http://www.omdbapi./?apikey=[apikey]&s=harry'
fetch(movieApi)
.then(data => data.json())
.then(movies => this.setState({movies}))
}
// <li>
// <img src="<%= movie['Poster'] %>">
// <b><%= movie['Title'] %></b> -
// <%= movie['Year'] %>
// </li>
render() {
let views = <div>Loading...</div>
const {movies} = this.state;
if(movies && movies.length > 0) {
views = movies.Search.map(m => (
<li key={m}>
<b>{m.Title}</b> - <strong>{m.Year}</strong>
</li>
))
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
{views}
</div>
);
}
}
export default App;
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
Share
Improve this question
asked May 25, 2018 at 21:24
seyiseyi
1211 gold badge1 silver badge12 bronze badges
2 Answers
Reset to default 1You are getting that error because your response is an Object, not an array. You have an array named Search in your object. Change your check condition like that:
if(movies.Search && movies.Search.length > 0)
But, I prefer setting the state differently and checking the condition in a simple way.
fetch( "http://www.omdbapi./?apikey=[api_key]&s=harry" )
.then( data => data.json() )
.then( json => this.setState( { movies: json.Search } ) );
Then in your ponent:
const { movies } = this.state;
if ( movies.length ) {
views = movies.map( m => (
<li key={m}>
<b>{m.Title}</b> - <strong>{m.Year}</strong>
</li>
) );
}
If nothing gets rendered make sure that your index.js file contains something like:
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));
But even after your App ponent loads the AJAX call returns
"{"Response":"False","Error":"Invalid API key!"}"
Therefore the if condition will never be true
and you won't see anything other than "Loading..."
本文标签: javascriptReact app rendering empty page with no error from the consoleStack Overflow
版权声明:本文标题:javascript - React app rendering empty page with no error from the console - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745413186a2657561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论