我想克隆/扩展阵营组分(不知道如果它是有状态或无状态),并通过它道具:阵营克隆组分(无状态或有状态)传递额外的道具
const Foo = (props) => {
return (
<div>foo</div>
);
}
class Bar extends React.Component {
render() {
return (
<div>bar</div>
)
}
}
问题是,这两个变量Foo
和Bar
应当区别处理:
const FooExtended = (props, context) => {
return Foo(_.extend(props, additionalProps), context);
}
class BarExtended extends Bar {
constructor(props, context) {
super(_.extend(props, additionalProps), context);
}
}
而且没有简单的方法来知道如果一个变量Component
无国籍或有状态没有做哈克toString
正则表达式测试。
React.cloneElement/createElement
失败,这些给我下面的错误:
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in.
那么,有没有一种简单的方法,我可以做只是cloneComponent(originalComponent, additionalProps)
?
https://shripadk.github.io/react/docs/clone-with-props.html检查了这一点 –
React.cloneElement'给我我张贴在这个问题 – Kousha