'use strict';
var Star = React.createClass({
getInitialState: function() {
return {x:30, y:30}
},
setPosition: function(x, y, refs) {
this.setState({x:x, y:y})
if (this.props.followedBy != null) {
setTimeout(()=>{
refs[this.props.followedBy].setPosition(x, y, refs)
}, 100);
}
},
render: function() {
return (
<div style={{position:'absolute', left:this.state.x, top:this.state.y, color:'orange'}}>
★
</div>
);
}
});
var Screen = React.createClass({
stars: 3,
onMouseMove: function(ev) {
this.refs._1.setPosition(ev.clientX, ev.clientY, this.refs)
},
componentDidMount: function() {
document.addEventListener('mousemove', this.onMouseMove);
},
componentWillUnmount: function() {
document.removeEventListener('mousemove', this.onMouseMove)
},
render: function() {
return <div>
<Star ref="_1" followedBy="_2"></Star>
<Star ref="_2" followedBy="_3"></Star>
<Star ref="_3" followedBy="_4"></Star>
<Star ref="_4" followedBy="_5"></Star>
<Star ref="_5" followedBy="_6"></Star>
<Star ref="_6" followedBy="_7"></Star>
<Star ref="_7" followedBy="_8"></Star>
<Star ref="_8"></Star>
</div>
}
});
ReactDOM.render(<Screen />, document.getElementById('container'));
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
External resources loaded into this fiddle: