Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda