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

198
Visualizações
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 Respostas
Responde à pergunta

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