将道具添加到节点对象

时间:2016-10-02 16:01:17

标签: reactjs react-native

我当前的组件正在接收一个节点作为其父节点的道具。

我们假设

myComponent.propTypes = {
  icon: PropTypes.node.isRequired,
}

图标将收到类似

的内容
<Icon src="...." description="..." />

现在在我的组件中我只需要添加

{this.props.icon}来渲染它。

我需要的是添加道具颜色=&#34;#f00&#34;以某种方式导致该节点

<Icon src="...." description="..." color="#f00"/>

我需要在myComponent中做。不确定这样做的正确语法是什么。

1 个答案:

答案 0 :(得分:1)

改为:

React.cloneElement( this.props.icon, { color: "#f00" } );

另一种选择是传递Icon组件而不是Icon个实例,以便React.createElement能够获得props个参数。

相关问题