伪类在 React 中未加载?

huangapple go评论73阅读模式
英文:

Pseudo-classes are not loaded in react?

问题

pseudo-classes are not displayed

'& > div': {
width: 'inherit',
marginTop: '100px',
backgroundColor: '#212121',
borderRight: 'none'

can you tell me how to fix it?

import useStyles from 'components/Sidebar/styles';

function MenuItem(props) {
const { title } = props;
return <Typography variant="body2">{title}</Typography>;
}

MenuItem.propTypes = {
title: PropTypes.object.isRequired
};

function SidebarItem(props) {
const { link, inset, toggleSidebar } = props;
const classes = useStyles();

function handleItemClick() {
if (isMobileDevice()) {
toggleSidebar();
}
}

return (
<Link to={link.path} className={classes.linkItem} tabIndex="-1">
<ListItem button onClick={handleItemClick}>
{link.icon && <ListItemIcon className={classes.listItemIcon}>{link.icon}</ListItemIcon>}
<ListItemText className={classes.listItemText} inset={inset} primary={link.title} />
</ListItem>
</Link>
);
}

SidebarItem.propTypes = {
link: PropTypes.object.isRequired,
inset: PropTypes.bool.isRequired,
toggleSidebar: PropTypes.func.isRequired
};

export default SidebarItem;


*applies to this file*

I did not write the imports part:

import React from 'react';
import PropTypes from 'prop-types';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';
import { Link } from '@reach/router';
import { isMobileDevice } from 'tools/mobileCheck.js';


What am I doing wrong? Tell me please.
I don&#39;t know what else to write)) but it&#39;s not enough for them.
Thank you, if you need more information, write to me.

```  listItemText: {
    paddingLeft: theme.spacing(1.5),
    &#39;&amp;:first-child&#39;: {
      paddingLeft: theme.spacing(5)
    }
  }

All pseudo-classes do not work((


<details>
<summary>英文:</summary>


import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => ({
sidebarList: {
transition: 'width 0.2s ease',
marginRight: '0px',
width: '300px',
'& > div': {
width: 'inherit',
marginTop: '100px',
backgroundColor: '#212121',
borderRight: 'none'
},
'&.mini': {
transition: 'width 0.2s ease',
width: '0px',
'& > div': {
width: 'inherit'
}
}
},
linkItem: {
textDecoration: 'none',
'&:focus': {
outline: 'none'
}
},
listItemIcon: {
marginRight: theme.spacing(1)
},
listItemText: {
paddingLeft: theme.spacing(1.5),
'&:first-child': {
paddingLeft: theme.spacing(5)
}
}
}));

export default useStyles;


pseudo-classes are not displayed

'& > div': {
width: 'inherit',
marginTop: '100px',
backgroundColor: '#212121',
borderRight: 'none'

can you tell me how to fix it?

import useStyles from &#39;components/Sidebar/styles&#39;;

function MenuItem(props) {
  const { title } = props;
  return &lt;Typography variant=&quot;body2&quot;&gt;{title}&lt;/Typography&gt;;
}

MenuItem.propTypes = {
  title: PropTypes.object.isRequired
};

function SidebarItem(props) {
  const { link, inset, toggleSidebar } = props;
  const classes = useStyles();

  function handleItemClick() {
    if (isMobileDevice()) {
      toggleSidebar();
    }
  }

  return (
    &lt;Link to={link.path} className={classes.linkItem} tabIndex=&quot;-1&quot;&gt;
      &lt;ListItem button onClick={handleItemClick}&gt;
        {link.icon &amp;&amp; &lt;ListItemIcon className={classes.listItemIcon}&gt;{link.icon}&lt;/ListItemIcon&gt;}
        &lt;ListItemText className={classes.listItemText} inset={inset} primary={link.title} /&gt;
      &lt;/ListItem&gt;
    &lt;/Link&gt;
  );
}

SidebarItem.propTypes = {
  link: PropTypes.object.isRequired,
  inset: PropTypes.bool.isRequired,
  toggleSidebar: PropTypes.func.isRequired
};

export default SidebarItem;

*applies to this file*

I did not write the imports part:

import React from &#39;react&#39;;

import PropTypes from 'prop-types';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';
import { Link } from '@reach/router';
import { isMobileDevice } from 'tools/mobileCheck.js';


What am I doing wrong? Tell me please.
I don&#39;t know what else to write)) but it&#39;s not enough for them.
Thank you, if you need more information, write to me.

```  listItemText: {
    paddingLeft: theme.spacing(1.5),
    &#39;&amp;:first-child&#39;: {
      paddingLeft: theme.spacing(5)
    }
  }

All pseudo-classes do not work((

答案1

得分: 2

The implementation of pseudo-classes directly within the generated CSS is not supported by the makeStyles function, which is the problem. You can get around this restriction by using the $pseudoClassName syntax.

Here's an example of how you can modify your styles to make the pseudo-classes work:

const useStyles = makeStyles(theme => ({
  sidebarList: {
    // Other styles...
    '& > div': {
      // Other styles...
      '&$mini': { // Use &$mini instead of &.mini
        // Mini styles...
      }
    }
  },
  mini: {}, // Define the mini class
  // Other styles...
}));

> Refer to [Docs][1]

[1]: https://mui.com/system/styles/advanced/#makestyles
英文:

The implementation of pseudo-classes directly within the generated CSS is not supported by the makeStyles function, which is the problem. You can get around this restriction by using the &$pseudoClassName syntax.

Here's an example of how you can modify your styles to make the pseudo-classes work:

const useStyles = makeStyles(theme =&gt; ({
  sidebarList: {
    // Other styles...
    &#39;&amp; &gt; div&#39;: {
      // Other styles...
      &#39;&amp;$mini&#39;: { // Use &amp;$mini instead of &amp;.mini
        // Mini styles...
      }
    }
  },
  mini: {}, // Define the mini class
  // Other styles...
}));

> Refer to Docs

huangapple
  • 本文由 发表于 2023年7月3日 21:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定