假设我有一个ReactElement
具有 className 的类,并且我想向其 className 添加一个新类,而不覆盖现有的 className。我该怎么做?
我尝试了以下操作,但它确实覆盖了现有的 className:
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 的一部分,并且应该在未来保持追溯兼容?我无法在文档中找到它。
有没有另一种方法来实现这一目标?