简体   繁体   English

ReactJs 使用状态数组 Map

[英]ReactJs useState Array Map

Hello I have doubts on how I can do this in react using useState, basically i have this menu where i need to map, i basically need a state containing all tags, and with boolean state true or false to know if the current item is active, and i will make it active by clicking on the item, and deactivate it when another item is clicked Hello I have doubts on how I can do this in react using useState, basically i have this menu where i need to map, i basically need a state containing all tags, and with boolean state true or false to know if the current item is active ,我将通过单击该项目使其处于活动状态,并在单击另一个项目时将其停用

that is, only one menu item active at a time也就是说,一次只有一个菜单项处于活动状态

export const SideBarTags = [
  {
    name: 'Tutoriais',
    link: '../tutorials',
    icon: faFileAlt,
    dropdownItems: null,
    active: false,
  },
  {
    name: 'Avisos',
    link: '../news',
    icon: faNewspaper,
    dropdownItems: null,
    active: false,
  },
  {
    name: 'Serviços',
    link: '../services',
    icon: faMeteor,
    active: false,
    dropdownItems: [
      { name: 'Elo Boost', link: '/eloBost' },
      { name: 'Duo Boost', link: '/duoBoost' },
      { name: 'MD10', link: '/eloBost' },
      { name: 'Coaching', link: '/duoBoost' },
      { name: 'Vitóriais', link: '/duoBoost' },
    ],
  },
  {
    name: 'Carteira',
    link: '../cartcredit',
    icon: faWallet,
    active: false,
    dropdownItems: [
      { name: 'Histórico', link: '/history' },
      { name: 'Adicionar Crédito', link: '/add' },
    ],
  },
];

and my TSX:和我的多伦多证券交易所:

const MenuTags: React.FC<Hamburguer> = ({ isOpen }) => {
    const [menuTags, setMenuTags] = useState(SideBarTags.map());

  return (
    <DashMenu open={isOpen}>
    <MenuItem /> //(this is my tag <li>
    </DashMenu>
  );
};



const MenuItem: React.FC = () => {


  return (
    <ListItem>
      <ListWrap

      >
        <a>
          <FontAwesomeIcon
            className="icon-li"
            icon={icon}
            size={isOpen ? 'lg' : 'lg'}
            fixedWidth
            color="white"
          />
          <span
            className="li-name"
          >
            {name}
          </span>
        </a>
    </ListItem>
  );
};

Super simplified version of what I did in a past project:我在过去项目中所做的超级简化版本:

const MenuTags = () => {
  const [selectedLink, setSelectedLink] = useState(null)

  return (
    <ul>
      {SideBarTags.map((obj) => (
        <li className={`${selectedLink === obj.name ? 'link--selected' : ''}`}>
          <a
            onClick={() => {
              setSelectedLink(obj.name)
            }}
            href={obj.link}
          >
            {obj.name}
          </a>
        </li>
      ))}
    </ul>
  )
}

Use CSS to open and close the menu items, by having a class such as 'link--selected' added to an element you can just show that item.使用 CSS 打开和关闭菜单项,通过将 class 添加到元素中,您可以只显示该项目。

Component logic if you wanted to map the menu items with the active item组件逻辑,如果你想 map 菜单项与活动项

const [menuItems, setMenuItems] = useState(SideBarTags);

const clickHandler = name => () => {
  setMenuItems(items =>
    items.map(item => ({
      ...item,
      active: item.name === name
    }))
  );
};

...

{menuItems.map(item => (
  <li
    key={item.name}
    className={item.active ? "active" : ""}
    onClick={clickHandler(item.name)}
  >
    {item.name}
  </li>
))}

CSS CSS

.active {
  // could be whatever style you need
  color: red;
}

编辑 blissful-elbakyan-fpzn9

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

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