Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I get the error trying to lift up props?

Here I have the state, and I am trying to pass them into child component SearchBar

App Component (parent component)

  // not related code ...

  // search
  const { search, setSearch } = useState("");

  const handleSearch = (e) => {
    setSearch(e.target.value);
  };
  
  // not related code ...

  return (
    <ContextProviders
      // not related code ...
    >
      <div className="App">
        <Navbar />
        <SearchBar search={search} handleSearch={(e) => handleSearch(e)} />
        <Outlet />
      </div>
    </ContextProviders>
  );
}

Into SearchBar when I am typing something in order to trigger setState it shows up an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SearchBar (child component)

import { useState } from "react";

export default function SearchBar(props) {
  // not related code ...

  console.log(props);

  return (
    <>
      <div>
        <input
          type="search"
          value={props.search}
          onChange={props.handleSearch}
          // not related code ...
        />
      </div>
    </>
  );
}```
I need to get the value from the input field in the search component(child) into App component(parent), I try to pass a function which sets state into App, but it doesn't work, I get the error: 
**ERROR**

Uncaught TypeError: setSearch is not a function
at handleSearch

Does anyone see the issue here?

>Solution :

You should initialize your state as:

const [search, setSearch] = useState("");

And pass the handleSearch function like this:

<SearchBar search={search} handleSearch={handleSearch} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading