use*_*386 5 components instantiation reactjs
我有一个表行组件,用于检查是否指定了自定义组件,并且应该以编程方式获取该 React 组件 ( column.customComponent) 并使用它渲染数据。但是,我似乎无法实例化组件。我得到的只是字符串形式的数据。
这是我所拥有的:
const cellContent = typeof(column.customComponent === 'undefined') ?
rowData[column.columnName] :
{
type: column.customComponent,
props: {
data: rowData[column.columnName],
rowData
}
};
Run Code Online (Sandbox Code Playgroud)
console.log(cellContent); 显示类似以下内容:
React 组件的函数表示
或
function DateCell() ....
我不确定我是否只是在分配时使用了错误的语法cellContent,或者我是否应该对工厂做一些事情或者什么。我该怎么做?
我使用这个辅助方法来创建组件并返回实例。
static async renderComponentAt(componentClass, props, parentElementId){
let componentId = props.id;
if(!componentId){
throw Error('Component has no id property. Please include id:"...xyz..." to component properties.');
}
let parentElement = document.getElementById(parentElementId);
return await new Promise((resolve, reject) => {
props.ref = (component)=>{
if(component){
resolve(component);
}
};
let element = React.createElement(componentClass, props, null);
ReactDOM.render(element, parentElement);
});
}
Run Code Online (Sandbox Code Playgroud)