gpt4 book ai didi

reactjs - React setState 获取 API

转载 作者:行者123 更新时间:2023-12-03 14:27:32 25 4
gpt4 key购买 nike

我现在开始学习 React 并创建我的第二个项目。我正在尝试使用 MovieDb API 创建电影搜索应用程序。当我得到最初的电影列表时,一切都很好。但是单击每个列表项时我想显示每部电影的详细信息。我使用 vanilla JS 和传统的 XHR 调用创建了一些这样的应用程序。这次我使用 fetch API,它看起来简单易用,但是当我映射响应数据以获取每部电影的 id 以便分别检索每部电影的详细信息时,我获得了所有项目的详细信息的完整列表,这不是想要的效果。我将对象列表放入数组中,因为在 map 中 setState 之后,我只获取最后一个元素的详细信息。我知道我可能在 API 调用中做错了什么,但这也可能是我的整个 REACT 代码。我将不胜感激任何帮助。

我的代码

App.js

import React, { Component } from 'react';
import SearchInput from './Components/SearchInput'
import './App.css';

class App extends Component {
constructor(props) {
super(props);

this.state =
{
value: '',
showComponent: false,
results: [],
images: {},
};

this.handleSubmit = this.handleSubmit.bind(this);
this.handleOnChange = this.handleOnChange.bind(this);
this.getImages = this.getImages.bind(this);
this.getData = this.getData.bind(this);
}

ComponentWillMount() {
this.getImages();
this.getData();
}

getImages(d) {
let request = 'https://api.themoviedb.org/3/configuration?api_key=70790634913a5fad270423eb23e97259'
fetch(request)
.then((response) => {
return response.json();
}).then((data) => {
this.setState({
images: data.images
});
});
}

getData() {
let request = new Request('https://api.themoviedb.org/3/search/movie?api_key=70790634913a5fad270423eb23e97259&query='+this.state.value+'');

fetch(request)
.then((response) => {
return response.json();
}).then((data) => {
this.setState({
results: data.results
});
});
}

handleOnChange(e) {
this.setState({value: e.target.value})
}

handleSubmit(e) {
e.preventDefault();
this.getImages();
this.setState({showComponent: true});
this.getData();
}

render() {
return (
<SearchInput handleSubmit={this.handleSubmit} handleOnChange={this.handleOnChange} results={this.state.results} images={this.state.images} value={this.state.value} showComponent={this.state.showComponent}/>
);
}
}

export default App;


SearchInput.js

import React, {Component} from 'react';
import MoviesList from './MoviesList';

class SearchInput extends Component {
render() {
return(
<div className='container'>
<form id='search-form' onSubmit={this.props.handleSubmit}>
<input value={this.props.value} onChange={this.props.handleOnChange} type='text' placeholder='Search movies, tv shows...' name='search-field' id='search-field' />
<button type='submit'>Search</button>
</form>
<ul>
{this.props.showComponent ?
<MoviesList value={this.props.value} results={this.props.results} images={this.props.images}/> : null
}
</ul>
</div>
)
}
}

export default SearchInput;

这是我尝试获取详细数据的组件

MovieList.js

import React, { Component } from 'react';
import MovieDetails from './MovieDetails';

let details = [];

class MoviesList extends Component {
constructor(props) {
super(props);

this.state = {
showComponent: false,
details: []
}

this.showDetails = this.showDetails.bind(this);
this.getDetails = this.getDetails.bind(this);

}


componentDidMount() {
this.getDetails();
}

getDetails() {
let request = new Request('https://api.themoviedb.org/3/search/movie?api_key=70790634913a5fad270423eb23e97259&query='+this.props.value+'');

fetch(request)
.then((response) => {
return response.json();
}).then((data) => {
data.results.forEach((result, i) => {
let url = 'https://api.themoviedb.org/3/movie/'+ result.id +'?api_key=70790634913a5fad270423eb23e97259&append_to_response=videos,images';
return fetch(url)
.then((response) => {
return response.json();
}).then((data) => {
details.push(data)
this.setState({details: details});
});
});
console.log(details);
});
}



showDetails(id) {
this.setState({showComponent: true}, () => {
console.log(this.state.details)
});
console.log(this.props.results)
}

render() {
let results;
let images = this.props.images;

results = this.props.results.map((result, index) => {
return(
<li ref={result.id} id={result.id} key={result.id} onClick={this.showDetails}>
{result.title}{result.id}
<img src={images.base_url +`${images.poster_sizes?images.poster_sizes[0]: 'err'}` + result.backdrop_path} alt=''/>
</li>
)
});

return (
<div>
{results}
<div>
{this.state.showComponent ? <MovieDetails details={this.state.details} results={this.props.results}/> : null}
</div>
</div>
)
}
}

export default MoviesList;

MovieDetails.js

import React, { Component } from 'react';

class MovieDetails extends Component {


render() {
let details;
details = this.props.details.map((detail,index) => {
if (this.props.results[index].id === detail.id) {
return(
<div key={detail.id}>
{this.props.results[index].id} {detail.id}
</div>
)} else {
console.log('err')
}
});

return(
<ul>
{details}
</ul>
)

}
}

export default MovieDetails;

最佳答案

这里发生了很多事情......

//在这里,您将附加一个 onclick 监听器,并触发您的“获取有关此特定电影功能的详细信息”,发送 id 或完整结果(如果您愿意)。

//然后你 getDetails 需要接受一个参数(id),你可以用它来获取一部电影。

getDetails(id){
fetch(id)
displayresults, profit
}

results = this.props.results.map((result, index) => {
return(
<li onClick={() => this.getDetails(result.id) ref={result.id} id={result.id} key={result.id} onClick={this.showDetails}>
{result.title}{result.id}
<img src={images.base_url +`${images.poster_sizes?images.poster_sizes[0]: 'err'}` + result.backdrop_path} alt=''/>
</li>
)
});

关于reactjs - React setState 获取 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com