[英]Converting stateless to a class component react js
I have a component here that fills up a data for movies: 我这里有一个组件,可以填充电影数据:
const MovieList = (props) => {
const movieItems = props.movies.map((movie) => {
return <MovieListItem key={movie._id} movie={movie} />
});
return(
<div className="container">
<h2 className="latest">MOVIE RESULTS</h2>
<div className="row">
{ movieItems }
</div>
</div>
);
};
Basically, I was planning to convert my next component where I am filling up my movieItems
to a class component. 基本上,我打算将我要填充movieItems
下一个组件转换为类组件。 This class has some helpers function along with a new function onClickImgMoreBtn
whenever it was click it must console log something. 此类在单击时必须具有一些辅助功能,同时还具有新的onClickImgMoreBtn
函数,它必须控制台记录某些内容。 But it doesnt work any more when I tried to convert it class component. 但是,当我尝试将其转换为类组件时,它不再起作用。 Even my helper classes wont work either. 甚至我的助手课程也不会工作。
Here's my original stateless function: 这是我最初的无状态功能:
import React from 'react';
const helper = require('../../helpers/helper_movies')
const MovieListItem = ({movie}) => {
return (
<div>
{helper.mappingComma(movie.genre)}
{helper.getExcerpt(movie.overview)}
<button className="btn btn-secondary float-right btn-sm" href={movie._id} onClick={this.onClickImgMoreBtn}>MORE</button>
</div>
);
};
export default MovieListItem;
And here's how i change it along with the new function: 这是我与新功能一起更改的方式:
import React, { Component } from 'react';
const helper = require('../../helpers/helper_movies');
class MovieListItem extends Component{
constructor(props){
super(props);
}
onClickImgMoreBtn(){
console.log('hi there!');
}
render(){
return (
<div>
{helper.mappingComma(this.props.genre)}
{helper.getExcerpt(this.props.overview)}
<button className="btn btn-secondary float-right btn-sm" href={this.props._id} onClick={this.onClickImgMoreBtn}>MORE</button>
</div>
);
}
}
export default MovieListItem;
Any idea what am i missing? 知道我想念什么吗? did i properly converted it? 我正确转换了吗?
In your stateless component you destructure movie from the props and use movie.genre,
movie._id
and movie.overview
and hence in the component you need to use this.props.movie.genre
, this.props.movie._id
and this.props.movie.overview
在无状态组件中,您可以从道具中movie.genre,
电影并使用movie.genre,
movie._id
和movie.overview
,因此在组件中,您需要使用this.props.movie.genre
, this.props.movie._id
和this.props.movie.overview
render(){
return (
<div>
{helper.mappingComma(this.props.movie.genre)}
{helper.getExcerpt(this.props.movie.overview)}
<button className="btn btn-secondary float-right btn-sm" href={this.props.movie._id} onClick={this.onClickImgMoreBtn}>MORE</button>
</div>
);
}
or better, destructure movie form props
in render 或更好,在渲染中destructure movie form props
render(){
const { movie } = this.props;
return (
<div>
{helper.mappingComma(movie.genre)}
{helper.getExcerpt(movie.overview)}
<button className="btn btn-secondary float-right btn-sm" href={movie._id} onClick={this.onClickImgMoreBtn}>MORE</button>
</div>
);
}
Also since you are not doing anything in the constructor, you need to have the constructor defined. 另外,由于您没有在构造函数中执行任何操作,因此需要定义构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.