3

https://www.emgoto.com/react-search-bar/

I am implementing the search bar from the link above.
But I get a typescript error and I tried to match the type, but it didn't work..

Homepage.tsx

const [searchQuery, setSearchQuery] = useState(query || '');

return(
<Search searchQuery={searchQuery} setSearchQuery={setSearchQuery} />
);

Search.tsx

import React, { FC } from 'react';

const Search: FC = ({ searchQuery, setSearchQuery }) => { // error!
  return (
    <form action="/" method="get">
      <label htmlFor="header-search">
        <span className="visually-hidden">Search blog posts</span>
      </label>
      <input
        value={searchQuery}
        onInput={(e) => setSearchQuery(e.target.value)}
        type="text"
        id="header-search"
        placeholder="Search blog posts"
        name="s"
      />
      <button type="submit">Search</button>
    </form>
  );
};

export default Search;

Error:

  1. Property 'searchQuery' does not exist on type '{ children?: ReactNode; }'.ts(2339)
  2. Property 'setSearchQuery' does not exist on type '{ children?: ReactNode; }'.ts(2339)
Taeeun Kim
  • 964
  • 1
  • 8
  • 20
  • 3
    `React.FC` is a *generic type* which accepts the type of the props as it's parameter. So you need to include the prop type, for example, like so: `const Search: FC<{searchQuery: string, setSearchQuery: (new_query: string) => void}> = ...` – CRice Jun 22 '21 at 16:20

1 Answers1

3

Comment from CRice

React.FC is a generic type which accepts the type of the props as its parameter. You'll need to include the prop type. Example:

const Search: FC<{searchQuery: string, setSearchQuery: (new_query: string) => void}> = ... 
Community
  • 1
  • 1
Taeeun Kim
  • 964
  • 1
  • 8
  • 20