【发布时间】:2015-10-30 11:35:31
【问题描述】:
假设我有一个ReactElement,它有一个类名,我想在它的类名中添加一个新类,而不覆盖现有的类名。
我该怎么做?
我尝试了以下方法,但它确实覆盖了现有的类名:
var hello = <Hello name="World" className="base"/>;
hello = React.cloneElement(hello,{className: "text1"});
hello = React.cloneElement(hello,{className: "text2"});
但是这个解决方案有效:
var hello2 = <Hello name="World" className="base"/>;
hello2 = React.cloneElement(hello2,{className: hello2.props.className + " test1"});
hello2 = React.cloneElement(hello2,{className: hello2.props.className + " test2"});
但是这样使用ReactElement.props 安全吗?它是 ReactElement 的公共 API 的一部分,并且应该在未来保持向后兼容吗?我在文档中找不到这个。
还有其他方法可以实现吗?
【问题讨论】:
-
改用共享变量? :P 我认为 ReactElement 将来会有道具是一个相当安全的赌注
标签: javascript reactjs