【发布时间】:2019-01-07 09:06:30
【问题描述】:
更新问题以澄清:我在这里尝试做的是对于索引 === 0 的每个实例,我想有条件地将黑色类 (recentTitleClass) 应用于排版文本。我通过道具将两个css类previousTitleClass和recentTitleClass传递到组件中。现在我只想将recentTitleClass 仅用于数组的第一个实例。如果添加了新的评论/标题,则此数组会发生变化,因此标记 previousTitleClass 和 recentTitleClass。
这是我目前所拥有的。
interface IProps {
comments?: List<Map<{}, {}>>;
previousTitleClass?: string;
recentTitleClass?: string;
}
type Props = IProps & WithStyles<typeof styles>;
class Component extends React.Component<Props> {
public render(): React.ReactNode {
const { comments } = this.props;
if (!comments || comments.count() <= 0) {
return null;
}
return comments.map((comment, index) => {
const shouldHaveClass = index === 0;
return (
comment && (
<React.Fragment key={index}>
{this.renderComment(comment, shouldHaveClass)}
</React.Fragment>
)
);
});
}
private renderComment(comment: Map<{}, {}>, shouldHaveClass:any) {
const { classes, previousTitleClass, recentTitleClass } = this.props;
const recentTitleClass = shouldHaveClass ? "commentFromOsbpSupport" : null;
let from: React.ReactNode;
switch (comment.getIn(["from", "role"])) {
case "ROLE_MENTOR":
from = (
<div>
<Typography
variant="body2"
className={classnames(
classes.commentFromMentor,
"comment-from comment-from--mentor",
previousTitleClass,
recentTitleClass
)}>
Mentor POC
</Typography>
</div>
);
break;
case "ROLE_OSBP_SUPPORT":
from = (
<Typography
variant="body2"
className={classnames(
classes.commentFromOsbpSupport,
"comment-from comment-from--osbp_support",
previousTitleClass,
recentTitleClass
)}>
Mentor Protégé Program Reviewer
</Typography>
);
break;
default:
from = (
<Typography variant="body2" className="comment-from">
Unknown Commenter
</Typography>
);
break;
}
--------------
该组件如何在另一个组件中使用
<CommentHistory comments={comments} previousTitleClass={classes.previousTitleClass} recentTitleClass={classes.recentTitleClass}/>
【问题讨论】:
-
comments[0]? -
你能澄清你的问题吗?
-
你能告诉我们你的
renderComment函数吗? -
@messerbill 更新了 renderComment 函数。我正在尝试做的是将 [0] 的每个第一次索引迭代设置为 #00000 的样式颜色 .... 之后的每次迭代都是灰色的。
-
刚刚更新了问题以供澄清
标签: javascript reactjs typescript react-native