首页 > 解决方案 > 反应克隆/创建元素

问题描述

当我可以在这样的配置中传递组件时,我有一个库(antUI):

const configuration = [{
  render: record => <Component record={record} />
}]

我想使用.map以下功能修改该配置:

const additionalProps = { someProp: 'value' };
configuration.map(el => ({
    render: el.render, // React.cloneElement? React.createElement?
}));

所以我可以将额外的道具传递给Component. 有没有办法做到这一点?我一直在尝试,React.cloneElementReact.createElement除了错误什么也没有。

标签: javascriptreactjs

解决方案


天哪,我()在代码末尾添加了不必要的内容。

它应该看起来像:

const additionalProps = { someProp: 'value' };
configuration.map((el) => {
    const copy = { ...el };
    return {
        ...el,
        render: record => React.cloneElement(copy.render(record), additionalProps),
    };
});

推荐阅读