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

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

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 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