简体   繁体   English

React + Typescript对象支持组件错误

[英]React + Typescript object props to component error

Simple case; 简单的情况; I'm rendering a list of 'Reviews'. 我正在呈现“评论”列表。 These are provided using the following Proptype: 使用以下属性类型提供这些:

export interface Props {
    title: string;
    name: string;
    reviewdesc: string;
    rating: number;
}

Mapping through the results in the parent component: 通过结果映射到父组件中:

{reviews.map((review: Props) => {
    return <Review data={review} />;
})}

And using the same Proptypes in the child component: 并在子组件中使用相同的Proptypes:

const Review = (data: Props) => { ...

It is giving me this error: 它给了我这个错误:

Type '{ data: Props; }' is not assignable to type 'IntrinsicAttributes & Props'.
  Property 'data' does not exist on type 'IntrinsicAttributes & Props'.

It feels like I'm forgetting a little thing. 感觉就像我忘了一件小事。 I thought I should catch the Props like {data} in the child component, but it gives: 我以为我应该在子组件中捕获{data}类的道具,但是它提供了:

Property 'data' does not exist on type 'Props'.

You are passing props incorrectly. 您传递道具的方式不正确。 Use, 采用,

<Review { ...review } />

... is called the spread operator and it "spreads" the properties of your object into props for that element. ...被称为散布运算符,它将对象的属性“散布”为该元素的道具。

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

相关问题 TypeScript React 组件在 useState 声明和将道具传递给子组件时出错 - TypeScript Error in React component on useState declaration and passing props to child component 使用 Typescript 在 React 组件上传播 props - Spreading props on a React component with Typescript React + TypeScript:将React组件作为道具传递,使用道具进行渲染[错误:TS2339] - React + TypeScript: Passing React Component as Props, rendering with props [Error: TS2339] 如何使用 react 和 typescript 访问组件中的道具? - How to access props in the component using react and typescript? 将道具传递给 Typescript 中的功能性 React 组件 - Passing props to functional React component in Typescript 如何使用 React TypeScript 将组件传递给道具? - How to pass component to props using React TypeScript? 将子项注入到 TypeScript 中由 props 传递的反应组件中 - Inject children into react component passed by props in TypeScript 将 onClick function 作为道具发送到带有 Typescript 的 React 组件 - Send onClick function as props to a component in React with Typescript TypeScript:有条件地在React组件中扩展道具 - TypeScript: Extend props in React component conditionally 打字稿:为通用React组件定义未声明的道具 - Typescript: Defining undeclared props for generic React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM