【发布时间】:2020-07-05 13:00:00
【问题描述】:
我是 React JS 的新手,遇到了一个问题。我试图通过 openPopup,它返回 openPopup 不是函数的错误。我已经完成了诸如在 MovieCard.JS 中解构 openPopup 之类的所有操作,但无济于事。我想传递点击卡片的 imbdID。有人可以帮忙吗? 谢谢你。 `
class Status extends Component {
state = {
isLoading: true,
movies: null,
search: '',
}
openPopup = id => {
console.log(id)
}
render() {
const { isLoading, movies } = this.state;
console.log(this.state);
return (
<div className='homapage'>
<h1>Find your favourite movies on IMDB</h1>
<div className='movies-result-cards'>
{!isLoading ? (
movies.map(movie=> <MovieCard key={movie.imdbID} {...movie} openPopup={this.openPopup} />)
): (movies === null && isLoading ? <h3> </h3> : <h3>...Loading </h3>)}
</div>
</div>
)}
}
`
import React from 'react';
import './MovieCard.css'
const MovieCard = (props, openPopup) => {
const { Title, Poster, Type, Year, imdbID} = props;
return (
<div className='card'>
<button onClick={() => openPopup(imdbID)}>View Order</button>
<h1>{Title.length > 20 ? Title.substring(0, 20) + '...' :
Title}</h1>
<img src={Poster} alt={Title}/>
<p>{Type}</p>
<p>{Year}</p>
</div>
)
}
export default MovieCard;
TypeError: openPopup is not a function
onClick
F:/practice/classified website/classified-site/src/Components/MovieCard.js:8
5 | const { Title, Poster, Type, Year, imdbID} = props;
6 | return (
7 | <div className='card'>
> 8 | <button onClick={() => openPopup(imdbID)}>View Order</button>
| ^ 9 | <h1>{Title.length > 20 ? Title.substring(0, 20) + '...' :
10 | Title}</h1>
11 | <img src={Poster} alt={Title}/>
【问题讨论】:
-
这不是你在子组件函数签名中解构道具的方式——为什么你对待函数道具的方式与其他所有不同?
标签: reactjs react-props react-component