製作將顯示彩色圓圈的React應用程序。但是當我添加一個for循環它停止工作。這裏錯了嗎?React中的JSX循環
我正在使用babel和JSX。這是從一本書練習 - 學習通過Kirupa Chinnathambi陣營
var colors = ["#393E41", "#E94F37", "#1C89BF", "#A1D363", "#85FFC7", "#297373", "#FF8552", "#A40E4C"];
var renderData = [];
// STOPED WORKING AFTER THIS LOOP
for (var i = 0; i < colors.length; i++) {
renderData.push(< Circle color = {colors[i]} />);
}
let Circle = React.createClass({
render: function() {
let styles = {
background: this.props.color,
width: 60,
height: 60,
borderRadius: "50%"
}
return (
<div style={styles}></div>
)
}
});
ReactDOM.render(
<div> {renderData} </div>, window.add);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="add"></div>
您是否嘗試在組件定義之後放置for循環? – Robsonsjre
OMG !!!!這是一個noob錯誤 –