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

166
Visualizações
React: Default Initial State with Prop Value

Default Initial State with Prop Value

activeTab is held in TabContext I cannot set the default state here because I don't know how to access the prop value of of Tab component in a TabContext component.

TabContext.js

const TabContext = createContext();

export function useTab() {
  return useContext(TabContext);
}

const TabProvider = ({ children }) => {
  const [activeTab, setActiveTab] = useState();

  const toggleTab = (title) => {
    setActiveTab(title);
  };

  return (
    <TabContext.Provider value={[activeTab, toggleTab]}>
      {children}
    </TabContext.Provider>
  );
};

export default TabProvider;

Usage

<TabProvider>
  <TabContainer>
     <Tab title='One' defaultTab/>
     <Tab title='Two' />
  </TabContainer>
</TabProvider>

Tab component

  • activeTab state is accessed by useTab() hook
  • toggleTab sets the state to whatever the input is.
  • I tried useEffect(), but since state changes it creates infinite loop and the activeTab is stuck on the title prop of Tab Component.
const Tab = ({ title, defaultTab }) => {
  const [activeTab, toggleTab] = useTab();

  useEffect(() => {
    if (defaultTab) {
      toggleTab(title);
    }
  }, []);

  return (
    <div
      className={(activeTab === title ? 'text-blue-600' : 'text-gray-900')}
      onClick={() => {
        toggleTab(title);
      }}
    >
      {title}
    </div>
  );
};

Desired Outcome: The activeTab default state is the title prop value of Tab component when it has the prop defaultTab while allowing state to be changed afterwards based on click. Also making defaultTab prop required for one of the Tab Components under TabContainer

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

0

Does Tab component can have multiple title or single value (Like 'One', 'Two') ? If so, it is correct to use useTab component in every Tab component. And if you are trying to make tab one selected by default you can change ur Tab component like below:


const Tab = ({ title, defaultTab }) => {
    const [activeTab, toggleTab] = useTab();
    return (
      <div
        className={(activeTab === title || defaultTab ? 'text-blue-600' : 'text-gray-900')}
        onClick={() => {
          toggleTab(title);
        }}
      >
        {title}
      </div>
    );
  };

As I understand you are trying to make any tab selected by default and change the state afterwards.

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