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

142
Visualizações
Children returned from FunctionComponent breaks Parent rendering logic

I created a FunctionComponent that should return conditionally the children that are envolved by it.

<Tabs defaultActiveKey='1' type='card' activeKey={'1'}>
    <TabPane tab='Tab title' key='1'>
        <p>Tab title</p>
    </TabPane>
    <WrapperComponent>
        <TabPane tab='Tab title' key='2'>
            <p> Tab title 2 </p>
        </TabPane>
    </WrapperComponent>
</Tabs>


const WrapperComponent: React.FC<Props> = ({children}) => {
    if(true)
        return (children)
    else
        return <></>
}

When the wrapper returns the children element, the UI breaks and don't render properly. Tabs are imported from antd.

The result from the page render: enter image description here

The same occurs on any element that are render whit some logic from antd UI library.

EDIT

I'm looking to find a way to render the child element based on some conditional, and after rendering it, force the parent element to "refresh" the UI, thus redoing the logic of creating the UI library component.

My goal in all of this is to create a component to validate that the logged in user has permission to render the object. With this, I seek to avoid the repetition of logic variables in the TSX file and obtain a more reusable and componentizable project architecture.

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

0

As you are trying to modularize the code. Move access control logic to a separate class, say ACL. This class doesn't need to be an UI component. Simple class will do.
In your UI component use class methods to determine if you want to show features or not.

Codepen demo
ACL.js

// access controll managment
var ACL = (function () {
  var role = "guest";
  var getRole = function () {
    return role;
  };

  var init = function () {
    //read local storage/ cookies and get role
    role = "admin";
  };

  var hasAccessTo = function (page, feature) {
    //do the checking
    return role === "admin";
  };

  // call this after login
  init();

  return {
    init: init,
    getRole: getRole,
    hasAccessTo: hasAccessTo
  };
})();

export default ACL;

In your UI component it can be used like this:

import ACL from "./ACL";


// filter based on role
function filter() {
  if (ACL.hasAccessTo("tabsPage", "secretTab")) {
    return (
      <TabPane tab="Secret" key="2">
        The access code is: ██████
      </TabPane>
    );
  }
}

const Demo = () => (
  <Tabs defaultActiveKey="1">
    <TabPane tab="Info" key="1">
      Genral info
    </TabPane>
     
    /* you can write a separate function to do the filtering */
    {filter()}

    /* OR use expression like this */
    {ACL.hasAccessTo("tabsPage", "secretTab") && 
      <TabPane tab="Secret 2" key="3">
        secret 2: some secret
      </TabPane> }
  </Tabs>

Your current WrapperComponent doesn't work because wrapped tabs are not getting processed properly. For example, id is rc-tabs-0-tab-null the key is missing.

about 4 years ago · Juan Pablo Isaza Relatório

0

what you can do is create a list of tabs and filter-based permission/roles.

const tabs = [
  {
    key: "1",
    tab: "Tab title",
    content: <p>Tab title</p>,
    permissions: ["admin", "user"],
  },
  {
    key: "2",
    tab: "Tab title 2",
    content: <p> Tab title 2 </p>,
    permissions: ["admin"],
  },
];

const WrapperComponent = ({ tabs, role }) => {
  return (
    <>
      {tabs.map(
        (tab) =>
          tab.permissions.include(role) ? (
            <TabPane tab={tab.tab} key={tab.key}>
              {tab.content}
            </TabPane>
          ): null
      )}
    </>
  );
};

<Tabs defaultActiveKey="1" type="card" activeKey={"1"}>
  <WrapperComponent tabs={tabs} role="admin" />
</Tabs>;
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