React:向现有组件添加道具 [英] React: adding props to an existing component
本文介绍了React:向现有组件添加道具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试弄清楚如何使用附加道具克隆现有元素.
I'm trying to figure out how to clone an existing element with additional props.
供参考:
this.mainContent = <Hello message="Hello world!" />
我尝试做类似的事情
React.createElement(this.mainContent, Object.assign({},
this.mainContent.props, { anotherMessage: "nice to meet ya!" }));
但它不起作用.
我将如何做到这一点?
推荐答案
您需要使用 React.cloneElement
例如:
You need to clone the element and add the additional props using React.cloneElement
e.g:
var clonedElementWithMoreProps = React.cloneElement(
this.mainContent,
{ anotherMessage: "nice to meet ya!" }
);
// now render the new cloned element?
这篇关于React:向现有组件添加道具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文