javascript - React + TypeScript: Passing React Component as Props, rendering with props [Error: TS2339] - Stack Overflow

I am using React + TypeScript.I have a scenario where I have to pass a React.SFC ponent to another pon

I am using React + TypeScript. I have a scenario where I have to pass a React.SFC ponent to another ponent. Which I am doing like this:

Container.tsx

import ChildComp from './ChildComp';
<ParentComponent CustomComp={ ChildComp } someArray={ [1, 2] } />

Now the issue is, I want to iterate this ChildComp ponent with someArray value, inside ParentComponent.

This child Component has it's own prop someValue, and I have added types to it, like what this child ponent can accept, and I am passing this prop while iterating it inside Parent.

ParentComponent.tsx

const content = someArray.map((value, index) => (
  <CustomComp someValue={ value } key={ index } />
));
{ content }

But I am getting error, that TS2339: Property 'someValue' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.

Although, if I directly use above iteration in Container.tsx where I am importing it, it works fine. But not working if I am passing it to another ponent. Am I missing something here?

Note: Since the same iteration is working on Container.tsx, I am passing the iterated content to ParentComponent and rendering it like a variable:

{ CustomComp }

This works, but I want to know why the other solution did not work.

More details

ChildComp.tsx

type Props = {
   someValue: number,
};

const ChildComp: React.SFC<Props> = ({ someValue }) => {
   return (
      // Content
      { someValue }
   )
}

Type of CustomComp, in ParentComponent:

type Props = {
   CustomComp?: React.ReactNode | null
};

Before it was React.ComponentType, but I was getting error, so I changed it ReactNode, since I am directly passing content now.

I am using React + TypeScript. I have a scenario where I have to pass a React.SFC ponent to another ponent. Which I am doing like this:

Container.tsx

import ChildComp from './ChildComp';
<ParentComponent CustomComp={ ChildComp } someArray={ [1, 2] } />

Now the issue is, I want to iterate this ChildComp ponent with someArray value, inside ParentComponent.

This child Component has it's own prop someValue, and I have added types to it, like what this child ponent can accept, and I am passing this prop while iterating it inside Parent.

ParentComponent.tsx

const content = someArray.map((value, index) => (
  <CustomComp someValue={ value } key={ index } />
));
{ content }

But I am getting error, that TS2339: Property 'someValue' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.

Although, if I directly use above iteration in Container.tsx where I am importing it, it works fine. But not working if I am passing it to another ponent. Am I missing something here?

Note: Since the same iteration is working on Container.tsx, I am passing the iterated content to ParentComponent and rendering it like a variable:

{ CustomComp }

This works, but I want to know why the other solution did not work.

More details

ChildComp.tsx

type Props = {
   someValue: number,
};

const ChildComp: React.SFC<Props> = ({ someValue }) => {
   return (
      // Content
      { someValue }
   )
}

Type of CustomComp, in ParentComponent:

type Props = {
   CustomComp?: React.ReactNode | null
};

Before it was React.ComponentType, but I was getting error, so I changed it ReactNode, since I am directly passing content now.

Share Improve this question edited Oct 16, 2018 at 7:20 Rahul Sagore asked Oct 16, 2018 at 5:58 Rahul SagoreRahul Sagore 1,6682 gold badges29 silver badges49 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

CustomComp prop isn't typed properly. It accepts a ponent (CustomComp={ ChildComp }), not an element (CustomComp={ <ChildComp /> }). In this case it would be not React.ReactNode but React.ComponentType.

It's also not random ponent but specific ponent that accepts someValue prop. ParentComponent props likely should be typed as:

type ParentCompProps = {
   CustomComp: React.ComponentType<ChildCompProps>,
   someArray: number[]
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744801243a4594501.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信