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

193
Vistas
Reuse component used in different context in react

I have a component which is using state & action from a context. In a different part of my app, I've created a different context but i want to reuse the same component. Can you please suggest any standard/elegant way to do so ?

import React from 'react'

const Sidebar = () => {
  const {state, action} = useDashboard();
  return (
    <div>...</div>
  )
}

export default Sidebar

Now i want to use this:

    const Sidebar = () => {
      const {state, action} = usePlotter();
      return (
        <div>...</div>
      )
    }
    
    export default Sidebar

useDashboard, usePlotter are hooks providing useContext().

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Move condition logic to inside another hook:

const useDashboardOrPlotter = (condition: boolean) => {
  const {stateDashboard, actionDashboard} = useDashboard();
  const {statePlotter, actionPlotter} = usePlotter();

  return condition ? {stateDashboard, actionDashboard} : {statePlotter, actionPlotter}
}

const Sidebar = () => {
  ... condition
  const {state, action} = useDashboardOrPlotter(condition);
  return (
    <div>...</div>
  )
}
about 4 years ago · Santiago Gelvez Denunciar

0

You can use like this in same component.

const {state: plottState, action: plottAction} = usePlotter();
const {state: dashboardState, action: dashboardAction} = useDashboard();

now use plottState, plottAction, dashboardState, dashboardAction

about 4 years ago · Santiago Gelvez 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