javascript - 反应克隆/创建元素
问题描述
当我可以在这样的配置中传递组件时,我有一个库(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.cloneElement
但React.createElement
除了错误什么也没有。
解决方案
天哪,我()
在代码末尾添加了不必要的内容。
它应该看起来像:
const additionalProps = { someProp: 'value' };
configuration.map((el) => {
const copy = { ...el };
return {
...el,
render: record => React.cloneElement(copy.render(record), additionalProps),
};
});
推荐阅读
- json - 没有键的结构/映射数组
- json - 如何在 API 调用完成后和组件呈现之前更新状态
- php - 检查数据库中是否有另一个用户名不起作用
- java - 不能在模仿 ArrayList 工作方式的类中的两个数组之间使用相等运算符
- r - 找到存在/不存在变量和定量变量之间的统计检验
- javascript - Svelte.js 组件属性在带有 customElement 的脚本标记中未定义:true
- json - 我们如何在 Flutter 应用程序中解析 json 响应的内部节点?
- java - 如何在sping jpa中使用转换器
- java - 如何交错两个数组列表?
- wordpress - 无法在 woocommerce 的订单页面上使用 query_posts 或 wp_query 获取帖子