I’m trying to display the latestMessageText variable as bold and black if the props meet a certain condition. The useEffect method works, an ugly solution; How do I use Material UI’s useStyle hook to execute my goal. I’ve tried passing props to useStyle, using them inside the makeStyles function, to no effect.
import React, { useEffect, useRef } from "react";
import { Box, Typography } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
justifyContent: "space-between",
marginLeft: 20,
flexGrow: 1,
},
username: {
fontWeight: "bold",
letterSpacing: -0.2,
},
previewText: {
fontSize: 12,
// color: "#9CADC8",
letterSpacing: -0.17,
color: (messages, otherUser) => {
if (messages.length && otherUser) {
const lastMessage = messages[messages.length - 1];
const { senderId, receiverHasRead } = lastMessage;
if (senderId === otherUser.id && !receiverHasRead) {
return 'black'
}
return "#9CADC8"
}
}
},
black: {
color: 'black',
fontWeight: 700,
}
}));
const ChatContent = (props) => {
const { conversation } = props;
const { latestMessageText, otherUser, messages } = conversation;
const classes = useStyles(messages, otherUser);
return (
<Box className={classes.root}>
<Box>
<Typography className={classes.username}>
{otherUser.username}
</Typography>
<Typography className={classes.previewText}>
{latestMessageText}
</Typography>
</Box>
</Box>
);
};
export default ChatContent;