setInterval
,但错误Error: Invariant Violation: EzLampComp.render(): A valid ReactComponent must be returned.
.ez-lamp{
display : inline-block;
margin : 5px;
width : 30px;
height : 30px;
border-radius : 50%;
}
.ez-lamp.on{
opacity : 1;
background : -webkit-radial-gradient(30% 30%,white 5%,red 95%);
}
.ez-lamp.off{
opacity : 0.5;
background : -webkit-radial-gradient(30% 30%,#888 5%,red 95%);
}
Ezlamp组件:
var EzLampComp = React.createClass({
render : function(){
var onoff = this.props.onoff;
setInterval(function(){
if(onoff="on")
return <span className = "ez-lamp off"></span>;
else
return <span className = "ez-lamp off"></span>;
},1000);
}
});
ReactDOM.render(
<EzLampComp onoff="on"/> , //JSX
document.querySelector("#content"));
答案 0 :(得分:3)
应该从componentDidMount调用setInterval,并且只能切换状态。
getInitialState() {
return {
onoff: this.props.onoff
}
}
componentDidMount() {
setInterval(function() {
this.setState({
onoff: !this.state.onoff
}, 1000);
}
}