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

118
Views
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
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!