Suppose I have a ReactElement
that has a className, and I want to add a new class to its className, without overriding the existing className.
How am I supposed to do this?
I've tried the following but it does override the existing className:
var hello = <Hello name="World" className="base"/>;
hello = React.cloneElement(hello,{className: "text1"});
hello = React.cloneElement(hello,{className: "text2"});
However this solution works:
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"});
But is this safe to use ReactElement.props
like that? Is it part of the public API of a ReactElement and is supposed to be kept retrocompatible in the future? I was not able to find this in the documentation.
Is there another way to achieve this?