我使用React.js構建了一個跳棋遊戲,並且我想要創建一個循環來呈現我的「Square」組件64次,以創建我的「Board」組件。在渲染函數中運行.map函數時,我能夠正確渲染Square組件。但是,我不喜歡在render函數中發生.map函數,並且想要調用一個單獨的函數,它在render函數中執行相同的操作。當我將.map函數移動到renderSquares函數中時,方塊不會被渲染。有人能解釋我在這裏錯過了什麼嗎?謝謝。在React.js中使用渲染函數外部的循環
import React, { Component } from "react";
import Square from "./Square";
class Board extends Component{
constructor(){
super()
this.state = {
myArray: Array(64).fill(null),
checked: false
}
console.log(this.state.checked)
}
renderSquares(){
this.state.myArray.map(function(obj, key){
return <Square key={key} checked={this.state.checked} />
}, this)
}
render(){
return(
<div className="wrapper">
{this.renderSquares()}
</div>
)
}
}
export default Board;
您只是缺少返回語句 – aw04