Javascript 类型字符串[]不可分配给类型';内在的颂词;字符串[]和#x27;
我正试图像这样传递Javascript 类型字符串[]不可分配给类型';内在的颂词;字符串[]和#x27;,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正试图像这样传递todos的道具,但我得到了一个错误: Type '{ todos: string[]; }' is not assignable to type 'IntrinsicAttributes & string[]'. Property 'todos' does not exist on type 'IntrinsicAttributes & string[]'.ts(2322) 我已经声明了一个上下文,但出于好奇,我想尝试使用道具,以便比较哪种性能更好 注意:
todos
的道具,但我得到了一个错误:
Type '{ todos: string[]; }' is not assignable to type 'IntrinsicAttributes & string[]'.
Property 'todos' does not exist on type 'IntrinsicAttributes & string[]'.ts(2322)
我已经声明了一个上下文
,但出于好奇,我想尝试使用道具,以便比较哪种性能更好
注意:
我已经尝试过使用这种技术:
const ListProps = {
todos
};
<List {...ListProps} />
如果使用Typescript和React,则需要显式地键入功能组件
const List = (todos: string[]) => {...}
首先,可以为组件定义类型别名或接口:
interface ListProps {
todos: string[]
}
然后,我们将列表组件键入为React.FC
,props界面作为泛型的一部分:
const List: React.FC<ListProps> = ({ todos }) => {...}
const List:React.FC=({todos})=>{…}
这将解决在父组件上导入列表时typescript编译器引发的错误。@jt25无论如何,请检查一下!我一定会调查的。根据你的经验,你更喜欢什么,钩子背景还是道具?@jt25我两者都用过,但我不认为我有一个明确的偏好:)我也不确定哪一个在性能上更好。