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

117
Visualizações
How can i use the state to filter the data and pass it to different components in React?

I'm trying to find a way to create a search component <SearchBar /> and display the result array through another component <TableData />. I managed to build it up to a certain point but is not showing the desired result. The problem is that is not shows the data until i start typing. When the parent component first loaded or i made a refresh, the data disappeared. Also when the parent component first loads, the filterProjects from useState render an empty array.

How can i display all the data when the components loads ?

Here's my code block:

const Overview = () => {

  const appCtx = useContext(AppContext);

  // Copy the incoming data from the context
  const newFilterCopy = [...appCtx.projects];

  // Create new state for filtered data
  const [filterProjects, setFilterProjects] = useState(newFilterCopy);

  // Function that takes the input coming from the SearchBar component
  const filterFunc = (input) => {
    if (input === '') {
      setFilterProjects(filterProjects);
    }

    const resultProjects = newFilterCopy.filter((project) =>
      project.projectCompanyName.toLowerCase().startsWith(input.toLowerCase())
    );
    setFilterProjects(resultProjects);
    return resultProjects;
  };

  

  return (
    <div className={classes.Overview}>
      <Container>
        
       <SearchBar
          placeholder='Search project by company name'
          onInput={filterFunc}
        />

        <div className={classes.TableContainer}>
          <ExportBtn />
          <TableData data={filterProjects} />
        </div>
      </Container>
    </div>
  );
};

export default Overview;
about 4 years ago · Juan Pablo Isaza
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