我有以下类型脚本代码:-
export default class SingleNews extends React.Component<INews, {}> {
public render(): React.ReactElement<INews> {
return (
<>
{this.props.featured
? this.props.recentNews.map(post => (
<a
className={styles.singleNews}
href={post.link}
key={post.Title}
>
<div
className={styles.singleNews__image}
style={{ backgroundImage: `url(${post.image})` }}
/>
<div className={styles.singleNews__content}>
<div className={styles.singleNews__content__info}>
<span className={styles.singleNews__content__info__label}>{post.Featured}</span>
<span className={styles.singleNews__content__info__date}>
{post.date}
</span></div>
</div>
<div className={styles.singleNews__content}>
<div className={styles.singleNews__content__info}>
<h2 className={styles.singleNews__content__info__title}>
{post.Title}
</h2>
{post.likes ? (
<div
className={styles.singleNews__content__info__reactions}
>
<span
className={
styles.singleNews__content__info__reactions__likes
}
>
<Icon iconName='Like' />
{post.likes}
</span>
<span>
<Icon iconName='ActionCenter' />
{post.coments}
</span>
</div>
) : null}
</div>
</div>
</a>
))
但我需要检查当前的索引并相应地呈现不同的html。。如果我尝试这个,我会得到这个错误:-
export default class SingleNews extends React.Component<INews, {}> {
public render(): React.ReactElement<INews> {
return (
<>
{this.props.featured
? this.props.recentNews.map((post,index) => (
if(index=0)
我得到了";期望表达式";在if
上?有什么建议吗?
问题是,您有一个语句作为箭头函数的主体(以前很好(。
由于现在你需要添加if来检查索引,你需要一个块:
{this.props.featured
? this.props.recentNews.map((post,index) => { // <-- this
if(index=0) {
// etc
}
return <a> .... </a>
}
}