我正在映射评论 API 的数组,当我点击“阅读更多”时,我只想显示点击的评论,但目前正在扩展我的数组的所有评论,我正在使用打字稿,这对我来说都是新的所以我不知道如何移动,我应该如何将索引的信息传递给我的状态?
interface State {
reviews: Review[];
isReadMore: boolean;
}
export default class MoviePage extends Component<{}, State> {
state: State = {
reviews: [],
isReadMore: false,
};
componentDidMount() {
this.asyncAwaitFunc();
this.toggle(arguments);
}
asyncAwaitFunc = async () => {
try {
const reviewmovie = await axios.get<ReviewResponse>(
`https://api.themoviedb.org/3/movie/${this.props.match.params.id}/reviews?api_key=`
);
this.setState({
reviews: reviewmovie.data.results,
});
} catch (error) {}
};
toggle(index: any) {
this.setState({
isReadMore: !this.state.isReadMore,
});
render() {
const { isReadMore, reviews } = this.state;
return (
<>
<ReviewGrid>
{reviews.map((review, index) => (
<ReviewContent key={index}>
{this.state.isReadMore
? review.content.substring(0, 650)
: review.content}
<Button onClick={() => this.toggle(index)}>
{isReadMore ? "...read more" : " show less"}
</Button>
</ReviewContent>
))}
</ReviewGrid>
</>
);
}
}