Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
How to make Sider selected key be responsive?

What I'm trying to do is get which is the key of the sider the user is using (without ReactDOM and with functional components)

my sample code here:

  export default function DrawerSider() {
  const history = useHistory();
  const [selectedKey, setSelectedKey] = useState("sub1")

  const handleSelectKey = function(key, history_string) {
    setSelectedKey(key)
    history.push(history_string)
    console.log(key)
  }

  return (
    <Sider width={200} className="site-layout-background">
      <Menu 
        defaultSelectedKeys={selectedKey}    
        mode="inline"
        style={{ height: "100%", borderRight: 0 }}
      >
        <Menu.Item
          key="sub1"
          icon={<HomeOutlined />}
          onClick={() => handleSelectKey("sub1","/dashboard/resumo")}
        >
          Dashboard
        </Menu.Item>
        <SubMenu key="sub2" icon={<UserOutlined />} title="Usuários">
          <Menu.Item
            key="1"
            icon={<PlusCircleOutlined />}
            onClick={() => handleSelectKey("1","/usuarios/novo")}
          >
            Adicionar usúario
          </Menu.Item>
          <Menu.Item
            key="2"
            icon={<TableOutlined />}
            onClick={() => handleSelectKey("2","/usuarios/todos")}
          >
            Todos usúarios
          </Menu.Item>
        </SubMenu>
        <SubMenu key="sub3" icon={<FormOutlined />} title="Formulários">
          <Menu.Item
            key="3"
            icon={<PlusCircleOutlined />}
            onClick={() => handleSelectKey("3","/formularios/novo")}
          >
            Adicionar formulário
          </Menu.Item>            
          </SubMenu>
        </SubMenu>
      </Menu>
    </Sider>
  );
}
  
  

Obs: I'm using ant design lib

Someone knows how to make it works?

I tried to use a const on click event to set a state of the selected key , but it didn't work

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is no onClick on Menu.Item

only Menu component has onClick or onSelect, both will give you callback of the "clicked" key, the differences is onClick is for any menu item click (including expanding menu) while on select is when you select an actual menu item.

There are 2 ways to get the history string:

1 - use the history string as menuItem key: the issue would be you cannot have 2 menu item that has same history key

2 - have a map which maps the menu key to history string

see below demo: https://codesandbox.io/s/inline-menu-antd-4-18-2-forked-bgwyj?file=/index.js

      const handleOnSelect = ({ item, key, keyPath, selectedKeys, domEvent }) => {
        console.log(item);
        console.log(keyPath);
        console.log(selectedKeys);
        console.log(domEvent);
        handleSelectKey(key);
      };

...
..
<Menu
  onSelect={handleOnSelect}
...
>
...
</Menu>
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to save the selected key as part of the component's state. You can set the current selected key as part of the onClick.

const selectedKey, setSelectedKey = useState("defaultSelectedKey")

Not sure what the useHistory hook is for but if you need to set that onClick as well as the selectedKey, move it all into one function like so

const handleSelectKey = function(key, history_string) {
  setSelectedKey(key)
  history.push(history_string)
}

...

onclick={() => handleSelectedKey("3", "/usuarios/novo")}

https://ant.design/components/menu/

EDIT

Based on research into your design library, your onclick and handle select must be at the top level of the menu component. Just copy the syntax that they use in their example code.

https://ant.design/components/menu/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda