Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!