简体   繁体   English

"<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>"

[英]TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }'

https://www.emgoto.com/react-search-bar/ 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主页.tsx

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

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

Search.tsx搜索.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;类型 '{ children?: ReactNode; 上不存在属性 'searchQuery' }'.ts(2339) }'.ts(2339)
  2. Property 'setSearchQuery' does not exist on type '{ children?: ReactNode;类型 '{ children?: ReactNode; 上不存在属性 'setSearchQuery' }'.ts(2339) }'.ts(2339)

Comment from CRice来自 Crice 的评论

React.FC is a generic type which accepts the type of the props as its parameter. React.FC是一个泛型类型,它接受 props 的类型作为其参数。 You'll need to include the prop type.您需要包含道具类型。 Example:例子:

const Search: FC<{searchQuery: string, setSearchQuery: (new_query: string) => void}> = ... 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 打字稿错误属性 x 在类型“只读”上不存在<Props> &amp; Readonly&lt;{ children?: ReactNode; }&gt;&#39; - Typescript error Property x does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>' 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' 将引用转发到 react.FC 给出类型错误:属性引用不存在于类型 &#39;IntrinsicAttributes &amp; Props &amp; { children :? 反应节点} - forwarding ref to react.FC gives type error: Property ref does not exist on type 'IntrinsicAttributes & Props & { children :? ReactNode} MUI v5 属性“fullWidth”在类型“IntrinsicAttributes &amp; { theme: Theme;”上不存在。 } &amp; { 孩子?:ReactNode; }&#39;。 TS2322 - MUI v5 Property 'fullWidth' does not exist on type 'IntrinsicAttributes & { theme: Theme; } & { children?: ReactNode; }'. TS2322 &#39;Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; 类型上不存在属性“导航” }&gt; - Property 'navigation' does not exists on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }> 类型不可分配给类型 'IntrinsicAttributes & { children?: ReactNode; }'。 财产 - Type is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'. Property 获取 TS 错误,“类型 &#39;TPage&#39; 上不存在属性 &#39;children&#39;” - Getting TS error, "Property 'children' does not exist on type 'TPage'" 类型 &#39;{ props: ReactNode; 上不存在属性 &#39;className&#39; }&#39; - Property 'className' does not exist on type '{ props: ReactNode; }' 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM