Update to question for clarification: What I'm trying to do here is for every instance where the index === 0, i want to conditionally apply a black class (recentTitleClass) to the typography text. I am passing the two css classes previousTitleClass & recentTitleClass into the component through props. Right now I only want to recentTitleClass used for only the first instance of the array. This array changes if a new comment/title is added, hence the labeling of previousTitleClass & recentTitleClass.
Here is what I have so far.
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;
}
--------------
how the component is being used in another component
<CommentHistory comments={comments} previousTitleClass={classes.previousTitleClass} recentTitleClass={classes.recentTitleClass}/>
comments[0]?renderCommentfunction ?