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

146
Visualizações
How to return an object conditionally

I'm working on a web App project and I have the following component that is returned by antd Menu object.

const items = [
        {
          label: <Link to='/register'>Register</Link>,
          key: 'register',
          icon: <UserAddOutlined />,
          style: { marginLeft: 1030},   // problem: hard-coded margin
        },
        {
          label: <Link to='/login'>Login</Link>,
          key: 'login',
          icon: <UserOutlined />,
          style: { marginLeft: 'auto'},
        },               
      ];

return <Menu mode="horizontal" items={items} />;

Now I have an object named user. If user is not null (which means there is a user who is logged in currently), I want to hide register in the above items object; otherwise I want to show register in the Menu. The Menu component will be looked like:

enter image description here

Can anyone help me with that?

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

0

You can insert this code after declaring items,

if(users!==null)
{
  items.splice(0,1) //removes first array element which is the register button
}

I would highly recommend you to use state for this purpose because that is the intended way for such cases.

By that I mean encapsulate the items object into a state and use the code above during every re-render using something like useeffect hook

Look at the question and the verified answer below to understand how to do it in newer versions of react

how to update an object using useEffect in Reactjs and Typescript

about 4 years ago · Juan Pablo Isaza Relatório

0

You just have to remember that items is a regular array. Not everything in React are exotic objects. A lot of your code will be using normal objects like strings, arrays, numbers etc. Don't be blinded by the array literal syntax that you forget that it's just an array.

There is already an answer showing what you can do to the items array to remove the first item. But you can also just write it simply:

let items = [];

if (user !== null) {
    items.push({
        label: <Link to='/register'>Register</Link>,
        key: 'register',
        icon: <UserAddOutlined />,
        style: { marginLeft: 1030},   // problem: hard-coded margin
    })
}

items.push({
    label: <Link to='/login'>Login</Link>,
    key: 'login',
    icon: <UserOutlined />,
    style: { marginLeft: 'auto'},
})

return <Menu mode="horizontal" items={items} />;
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