简体   繁体   English

如何使用 Material UI useStyles/makeStyles 有条件地渲染 CSS?

[英]How do I conditionally render CSS with Material UI useStyles/makeStyles?

I'm trying to display the latestMessageText variable as bold and black if the props meet a certain condition.如果道具满足特定条件,我正在尝试将 latestMessageText 变量显示为粗体和黑色。 The useEffect method works, an ugly solution; useEffect 方法有效,一个丑陋的解决方案; How do I use Material UI's useStyle hook to execute my goal.如何使用 Material UI 的 useStyle 挂钩来执行我的目标。 I've tried passing props to useStyle, using them inside the makeStyles function, to no effect.我尝试将道具传递给 useStyle,在 makeStyles function 中使用它们,但没有效果。

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;

I recommend here using A tiny (228B) utility for constructing className strings conditionally which is clsx .我建议在这里使用A tiny (228B) 实用程序来有条件地构造 className 字符串,即clsx

I provided solution here.我在这里提供了解决方案。

First method with {[yourclass]: conditon} syntax:第一种使用{[yourclass]: conditon}语法的方法:

import React, { useEffect, useRef } from "react";
import { Box, Typography } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import clsx from 'clsx';

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,
  },
  black: {
    color: 'black',
    fontWeight: 700,
  }
}));

const ChatContent = (props) => {
  const classes = useStyles();
  const typeographyEl = useRef();

  const { conversation } = props;
  const { latestMessageText, otherUser, messages } = conversation;

  const lastMessage = messages?.length && messages[messages.length - 1];
  const { senderId, receiverHasRead } = lastMessage;

  return (
    <Box className={classes.root}>
      <Box>
        <Typography className={classes.username}>
          {otherUser.username}
        </Typography>
        <Typography ref={typeographyEl} className={clsx(classes.previewText, {[classes.black]: senderId === otherUser.id && !receiverHasRead})}>
          {latestMessageText}
        </Typography>
      </Box>
    </Box>
  );
};

export default ChatContent;

Or second method with {conditon && yourstyle} syntax:或者使用{conditon && yourstyle}语法的第二种方法

 <Typography ref={typeographyEl} 
             className={clsx(classes.previewText, {(senderId === otherUser.id && !receiverHasRead) && classes.black})}>
          {latestMessageText}
 </Typography>

That's not the React way of handling this sort of function.这不是处理这种 function 的 React 方式。 You need to get the condition in a state variable (if you expect it to change by user interaction) or a constant variable otherwise, and set the class based on that.您需要在 state 变量(如果您希望它通过用户交互更改)或常量变量中获取条件,并在此基础上设置 class。

const [messageRead, setMessageRead] = useState(false);
useEffect(() => {
  if (messages.length) {
    const { senderId, receiverHasRead } = messages[messages.length - 1];
    setMessageRead(senderId === otherUser.id && !receiverHasRead);
  }
}, [messages, otherUser]);

JSX JSX

<Typography ref={typeographyEl} className={classes.previewText + messageRead ? ` ${classes.black}` : ''}>
  {latestMessageText}
</Typography>

You can use the classnames or clsx npm package to simplify the ternary condition.您可以使用名或clsx npm package 来简化三元条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM