Im new to react so im not sure whats going wrong here... Im trying to create a 3x3 structure of input boxes. This is what i came up with
function Square(prop){
return (<input type = 'text' class='w-25' style="display:inline-block">${prop.key}</input>);
}
function Row(props){
const row =[];
for( let i=props.key*3;i<(props.key+3);i++){
row.push(<Square key ={i}/>);
}
return (row);
}
class Box extends React.Component{
constructor(props){
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}
render(){
let board =[];
for( let i=0; i<3;i++){
board.push(<div><Row key={i} /></div>);
}
return (board);
}
}
ReactDOM.render(
<Box />,
document.getElementById('root')
);
But I am getting the error
Warning: Each child in an array or iterator should have a unique "key" prop.
As far as my Dom goes the 3 rows have been rendered as
Any help would be greatly appreciated
UPDATE
After trying out some answers given below I've ended up with a single row of 4 input boxes... although I've only specified 3 in the loop. Why is this can some one explain and I've specified 2 more rows.This is the new code
function Square(prop){
return (<input type = 'text' id={prop.value} value={'x'}></input>);
}
function Row(props){
const row =[];
for( let i=props.value*3;i<(props.value+3);i++){
const val = `${i}input`;
const key = `${i}square`;
row.push(<Square key ={key} value={val}/>);
}
return (row);
}
class Box extends React.Component{
constructor(props){
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}
render(){
let board =[];
for( let i=0; i<3;i++){
const key = `${i}row`;
board.push( <Row key={key} value={i}/>);
console.log(i)
}
return (board);
}
}
ReactDOM.render(
<Box />,
document.getElementById('root')
);
for( let i=props.key*3;i<(props.key+3);i++)Are you sure this is working?