admin管理员组文章数量:1406308
In a DataGridCell ponent I need to provide ability to point which ponent should be render content of the cell. Also props needs to be passed into this ponent. I tried to do it in the next way (simplified version):
import * as React from 'react';
interface IDataGridCellProps {
data?: any,
ponent?: React.Component,
}
export default class DataGridCell extends React.Component<IDataGridCellProps> {
public render() {
const Cmp: React.Component<any> = this.propsponent as React.Component;
return (
<div>
<Cmp {...this.props.data} />
</div>
)
}
}
I'm getting next error: TS2604: JSX element type 'Cmp' does not have any construct or call signatures
What is wrong and what is the correct way to render dynamic ponent?
UPD
I use then DataGridCell
ponent like this:
<DataGridCell key={indexCell}
data={profile}
ponent={cellponent}
/>
This is in a loop. cellponent
is in the config and looks like this: ponent: Text
. Text
is our ponent.
UPD 2
So it looks like a problem not in the implementation but in ts-lint
and typescript
. I cast ponent to type any
and it works now.
Changed line: const Cmp: any = this.propsponent;
Any more valuable explanation is appreciated.
In a DataGridCell ponent I need to provide ability to point which ponent should be render content of the cell. Also props needs to be passed into this ponent. I tried to do it in the next way (simplified version):
import * as React from 'react';
interface IDataGridCellProps {
data?: any,
ponent?: React.Component,
}
export default class DataGridCell extends React.Component<IDataGridCellProps> {
public render() {
const Cmp: React.Component<any> = this.props.ponent as React.Component;
return (
<div>
<Cmp {...this.props.data} />
</div>
)
}
}
I'm getting next error: TS2604: JSX element type 'Cmp' does not have any construct or call signatures
What is wrong and what is the correct way to render dynamic ponent?
UPD
I use then DataGridCell
ponent like this:
<DataGridCell key={indexCell}
data={profile}
ponent={cell.ponent}
/>
This is in a loop. cell.ponent
is in the config and looks like this: ponent: Text
. Text
is our ponent.
UPD 2
So it looks like a problem not in the implementation but in ts-lint
and typescript
. I cast ponent to type any
and it works now.
Changed line: const Cmp: any = this.props.ponent;
Any more valuable explanation is appreciated.
Share Improve this question edited Oct 2, 2018 at 10:58 mykhailovskyi asked Oct 2, 2018 at 10:03 mykhailovskyimykhailovskyi 6801 gold badge9 silver badges19 bronze badges 3-
How are you using
DataGridCell
? What are you passing in as theponent
prop? – Matt Way Commented Oct 2, 2018 at 10:10 -
If
this.props.ponent
is already a ponent, you just do<div>{this.props.ponent}</div>
But if you want to pass props to this, you need to pass a class not a ponent,.. IOW, from the calling side you would do -><DataGridCell ponent={MyComponent}/>
and not<DataGridCell ponent={<MyComponent/>}
– Keith Commented Oct 2, 2018 at 10:20 - @MattWay, please have a look at the updated question. – mykhailovskyi Commented Oct 2, 2018 at 10:46
1 Answer
Reset to default 4It should be done like this:
interface IDataGridCellProps
{
data?: any;
ponent?: React.ComponentType<any>;
}
export default class DataGridCell extends React.Component<IDataGridCellProps> {
public render()
{
const Cmp = this.props.ponent;
if (Cmp)
{
return (
<div>
<Cmp {...this.props.data} />
</div>
);
}
return null;
}
}
TypeScript now handles properly generics in jsx so it can be:
interface IDataGridCellProps<T>
{
data?: T;
ponent?: React.ComponentType<T>;
}
export default class DataGridCell<T> extends React.Component<IDataGridCellProps<T>> {
public render()
{
const Cmp = this.props.ponent;
if (this.props.data === undefined || Cmp === undefined)
{
return null;
}
return (
<div>
<Cmp {...this.props.data} />
</div>
);
}
}
本文标签: javascriptReactJS Render dynamic Component and pass propsStack Overflow
版权声明:本文标题:javascript - ReactJS: Render dynamic Component and pass props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744998361a2636820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论