【发布时间】:2019-06-22 07:13:17
【问题描述】:
I am new to React and trying to understand the basics. I have a child component called movieList that return a list of movies in a table form.
const movieList = () =>{
const {movies} = props;
return (){
<tbody>
<Fragment>
{movies.map((movie, i) => (
<tr key={i}>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
<td><button className="btn btn-danger btn-sm" onClick={props.onClick} movie={props.movie}>Delete</button></td>
</tr>
))}
</Fragment>
</tbody>
} }
And the Parent
class Movies extends Component {
constructor(props) {
super(props);
this.state = { movies: getMovies(), }
}
handleDelete = (movie) => {
console.log(movie);
}
render() {
return (
<MoviesList movies={this.state.movies} onClick={() => this.handleDelete(movie)} />
);
}
}
我在桌子上正确地得到了电影列表。但我想在按钮单击时将按钮作为删除目标。我知道我需要将电影作为子组件movieList的道具传递,但我似乎无法弄清楚如何传递电影以便我能够删除。使用 onClick 属性上调用的 handleDelete 函数,我在控制台上得到一个未定义的值。而我希望在点击时获得电影的价值。我如何在 React 中做到这一点。
【问题讨论】:
-
嗨 Olasunkanmi,试试下面的解决方案,如果有帮助,请告诉我:)
标签: javascript reactjs jsx