流程如下:
单击CarouselItem
,这会触发onClick={this.handleId}
。
handleId
带有ID,它是单击的CarouselItem
this.props.dispatch(fetchRecon(movieId));
将执行AJAX操作,该操作将返回数据
,并在Redux状态下,将最初为moviesRecon:[]
的属性设置为moviesRecon:[data]
。 handleId
还将使用this.props.history.push(`/movie-detail?query=${movieId}`);
我现在应该在组件MovieDetailPage
上,该组件当前正在为Carousel
组件使用模拟数据。理想情况下,我应该通过this.props.moviesRecon
来获得Carousel
的电影道具。
尝试这样做会给我以下错误:
TypeError: Cannot read property 'map' of undefined
const carousel = this.props.movies.map((item, index) => {
| ^ 51 | return (
52 | <CarouselItem data={item} index={index} key={this.props.movies[index].id} id={this.props.movies[index].id} />
53 | );
以下是我认为相关的组件: CarouselItem.js
import React from 'react';
import './CarouselItem.css';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { fetchRecon } from '../../actions/index';
import { withRouter } from 'react-router-dom';
export class CarouselItem extends React.Component{
handleId = e => {
let movieId = this.props.id
console.log("carousel item", movieId);
this.props.dispatch(fetchRecon(movieId));
this.props.history.push(`/movie-detail?query=${movieId}`);
}
render() {
let genres = this.props.genres;
let genreList = this.props.genres.length;
function getGenreText(id) {
for (let i = 0; i < genreList; i++) {
if (genres[i].id == id) {
return genres[i].name;
}
}
}
if (this.props.data) {
return (
<div className="movieContainer" onClick={this.handleId}>
<img className="moviePoster" src={`https://image.tmdb.org/t/p/w500/${this.props.data.poster_path}`} alt={this.props.data.title} />
<h3 className="movieName">{this.props.data.title}</h3>
<p className="movieGenre">{getGenreText(this.props.data.genre_ids[0])}</p>
</div>
);
}
}
}
const mapStateToProps = state => ({
genres: state.app.genres
});
export default connect(mapStateToProps)(withRouter(CarouselItem));
Carousel.js
import React from 'react';
import CarouselItem from '../CarouselItem/CarouselItem';
import './Carousel.css';
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
export class Carousel extends React.Component {
componentDidMount() {
// the trick
setTimeout(() => {
this.forceUpdate();
}, 50);
}
render(){
console.log("carousel probs", this.props);
const carousel = this.props.movies.map((item, index) => {
return (
<CarouselItem data={item} index={index} key={this.props.movies[index].id} id={this.props.movies[index].id} />
);
});
return (
<div className="inner-carousel">
<h2>{this.props.title}</h2>
<Slider {...settings}>
{carousel}
</Slider>
<hr className="carousel-divider"/>
</div>
)
}
}
MovieDetailPage.js
import React from 'react';
import { connect } from 'react-redux';
import { Carousel } from '../Carousel/Carousel';
import { withRouter } from 'react-router-dom';
import {fetchRecon} from '../../actions/index';
import { recommended } from './mock';
import { movieDetails } from './mockdetails';
import './MovieDetailPage.css';
export class MovieDetailPage extends React.Component {
componentDidMount() {
window.scrollTo(0, 0);
window.onpopstate = () => {
const params = (new URL(document.location)).searchParams;
const clickedMovie = params.get("query");
console.log('here', clickedMovie);
}
const params = (new URL(document.location)).searchParams;
const movieId = params.get("query");
console.log('here', movieId);
// this.props.dispatch(fetchRecon(movieId));
}
render() {
console.log('movie detail', this.props.moviesRecon, this.props)
return (
<div className="movieDetail">
<div className="movieDetail-container">
<img className="movieDetail-bg" src={`https://image.tmdb.org/t/p/original/or06FN3Dka5tukK1e9sl16pB3iy.jpg`} />
</div>
<div className="main-details">
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={recommended} />
<Carousel title={"Recommended"} movies={this.props.moviesRecon} />
</div>
</div>
);
}
}
const mapStateToProps = state => ({
isLoading: state.app.isLoading,
moviesRecon: state.app.moviesRecon
});
export default connect(mapStateToProps)(withRouter(MovieDetailPage));
对于29行的MovieDetailPage.js,我正在控制台中记录以下内容:
movie detail undefined
{history: {…}, location: {…}, match: {…}, staticContext: undefined}
history: {length: 17, action: "PUSH", location: {…}, createHref: ƒ, push: ƒ, …}
location: {pathname: "/movie-detail", search: "?query=456740", hash: "", state: undefined, key: "dyth5r"}
match: {path: "/movie-detail", url: "/movie-detail", isExact: true, params: {…}}
staticContext: undefined
__proto__: Object
Reducers.js,如有必要,但我已经确认了该操作,并且reducer正确设置了状态,因为其他组件也可以访问movieRecon。
const initialState = {
isLoading: 0,
genres: [],
moviesPlaying: [],
moviesPopular: [],
moviesUpcoming: [],
moviesTop: [],
movieSearch:[],
moviesRecon: [],
searchTerm: ""
};
export const Reducer = (state=initialState, action) => {
if (action.type === FETCH_RECON_SUCCESS) {
return Object.assign({}, state, {
moviesRecon: action.recon
});
}
}
答案 0 :(得分:0)
由于此行,您收到了错误消息,
<Carousel title={"Recommended"} movies={recommended} />
您正在传递组件recommended
,可以看到您使用import { recommended } from './mock';
导入了该组件。
在Carousel
组件中,您尝试使用movies
遍历map
数组。
这就是为什么会出错的原因,因为recommended
是component
,而不是是array
。
尝试将array
传递给所有子组件。
答案 1 :(得分:0)
我认为您在组件有足够的时间来获取电影数据之前启动.map
。您可以执行以下操作进行检查:
Carousel.js
import React from 'react'
import CarouselItem from '../CarouselItem/CarouselItem'
import './Carousel.css'
import Slider from 'react-slick'
import 'slick-carousel/slick/slick.css'
import 'slick-carousel/slick/slick-theme.css'
export class Carousel extends React.Component {
componentDidMount() {
// the trick
setTimeout(() => {
this.forceUpdate()
}, 50)
}
createCarousels = () => {
const movies = this.props.movies
if (movies.length > 0) {
return movies.map((item, index) => {
return (
<CarouselItem
data={item}
index={index}
key={this.props.movies[index].id}
id={this.props.movies[index].id}
/>
)
})
}
}
render() {
return (
<div className="inner-carousel">
<h2>{this.props.title}</h2>
<Slider {...settings}>{this.createCarousels()}</Slider>
<hr className="carousel-divider" />
</div>
)
}
}
在旁注中,您似乎正在尝试将settings
对象散布到Slider
组件中,但始终没有定义设置。应该调查一下。
答案 2 :(得分:0)
好,所以我知道了。我基本上在更高级别的App.js组件中输入了导入错误,这就是为什么它没有连接的原因。我试图导入名为MovieDetailPage的MovieDetailPage的成员,而不是默认导出,这是我需要的已连接组件。所以错了...
我有{ MovieDetailPage }
,应该是MovieDetailPage