动态更改节点元素的颜色,尝试从React.Children.map
更改字符串变量,然后React.cloneElement
更改颜色。我的主页上有一个标题。我想更新标题字符串中每个字符的节点颜色。试图做这种方式,但得到恩错误:Cannot read property 'colorString' of undefined
在ReactJs中,通过循环字符串
const LetterStyle = React.createClass({
colorString: function(value){
this.setState({color: "green"});
},
render: function(){
const colorArray = []
var childrentWithProps = React.Children.forEach(this.props.children, (child, i) => {
for (var i=0; i<child.length; i++){
return React.Children.map(child[i], function(content, i) {
if(content !== " "){
React.cloneElement(content, { colorString: this.colorString })
}
});
}
})
return(
<h1 className="lead" ref="lead">
<span>{childrentWithProps}</span>
</h1>
);
}
});