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

197
Views
React - Share state between child and hook

I have a screen A, a component B and a hook C.

The screen A renders the component B and executes its logic via the hook C.

Currently, in B I am rendering a list of pressable items:

function FilmCategoriesFilter({ // This is the component B
  defaultSelectedFilter = FILM_CATEGORIES[0],
}) { 
  const [selectedFilter, setSelectedFilter] = useState(FILM_CATEGORIES);
 
  return (
    <PressableList 
      data={FILM_CATEGORIES} 
      selectedItem={selectedFilter} 
      onPressItem={setSelectedFilter} 
    />
  );
}

In my hook C I need to receive a param category, which has to be the same as the B's state value.

I have thought to move the state from the component to the parent, and update it via callback. But, if I do it, I think the component B will not be reusable any more.

I mean, if I handle its state, the data which makes it work, outside of it, the component will pass from being a self-functional component to just a UI component which only works in the screen A.

Is this an anti-pattern? Any tips?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Having the main view for a particular state be somewhat separated from the functionality that manipulates and changes that state is fine and normal, especially when the state needs to be shared among other components too.

One approach you can use for this situation to make it clear that a state is directly tied to a component but can be used elsewhere too would be to declare both the hook and the component in the same file, export both, and nothing else.

export const useSelectedFilter = (defaultSelectedFilter = FILM_CATEGORIES[0]) => {
  const [selectedFilter, setSelectedFilter] = useState(FILM_CATEGORIES);
  // put methods that manipulate the state here
  // then return an object with those methods along with the state and setter
  const doSomethingWithSelectedFilter = () => {
    // ...
  };
  return { selectedFilter, setSelectedFilter, doSomethingWithSelectedFilter }; 
};

export function FilmCategoriesFilter({
  selectedFilter,
  setSelectedFilter,
}) {
  return (
    <PressableList 
      data={FILM_CATEGORIES} 
      selectedItem={selectedFilter} 
      onPressItem={setSelectedFilter} 
    />
  );
}
// end of file

This lets you use useSelectedFilter in a parent component, while keeping it absolutely clear that useSelectedFilter implements logic primarily for use by FilmCategoriesFilter. It's still modular and reusable due to the custom hook.

about 4 years ago · Juan Pablo Isaza 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!