JavaScript
x
43
43
1
function Circle(props) {
2
return(<span className='Circle'>this a circle</span>)
3
}
4
5
class Lottery extends React.Component{
6
constructor(props){
7
super(props);
8
this.state = {};
9
}
10
11
render(){
12
let circles = () => {
13
let circlesrow = [];
14
for (let i;i < this.props.n;i++) {
15
circles.push(<Circle/>)
16
}
17
return (circlesrow);
18
}
19
return(
20
<div>
21
<div>
22
{circles()}
23
</div>
24
</div>
25
);
26
}
27
}
28
29
30
function App() {
31
return (
32
<div className="App">
33
<Lottery n={9}/>
34
<h1>Hello CodeSandbox</h1>
35
<h2>Start editing to see some magic happen!</h2>
36
</div>
37
);
38
}
39
40
ReactDOM.render(
41
<App />,
42
root
43
);
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
3
4
<div id="root"></div>
i wanted to insert 9 components in Lottery components.but nothing shows up. can someone just explain to me why a relationship like this doesn’t work in React? and what is the wrong practice used here?
Advertisement
Answer
- Init
i
in the for loop with a0
–i = 0
- push the circles into the row –
circlesrow.push(<Circle />)
- Call the function –
{circles()}
JavaScript
1
42
42
1
function Circle(props) {
2
return(<span className='Circle'>this a circle</span>)
3
}
4
5
class Lottery extends React.Component{
6
state = {};
7
8
render(){
9
const circles = () => {
10
const circlesrow = [];
11
12
for (let i = 0; i < this.props.n; i++) {
13
circlesrow.push(<Circle />)
14
}
15
16
return circlesrow;
17
}
18
19
return(
20
<div>
21
<div>
22
{circles()}
23
</div>
24
</div>
25
);
26
}
27
}
28
29
function App() {
30
return (
31
<div className="App">
32
<Lottery n={9}/>
33
<h1>Hello CodeSandbox</h1>
34
<h2>Start editing to see some magic happen!</h2>
35
</div>
36
);
37
}
38
39
ReactDOM.render(
40
<App />,
41
root
42
)
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
3
4
<div id="root"></div>
You can also generate the circles’ array with Array.from()
direcly:
JavaScript
1
34
34
1
function Circle(props) {
2
return(<span className='Circle'>this a circle</span>)
3
}
4
5
class Lottery extends React.Component{
6
state = {};
7
8
render(){
9
const { n } = this.props;
10
11
return(
12
<div>
13
<div>
14
{Array.from({ length: n }, (_, i) => <Circle key={i} />)}
15
</div>
16
</div>
17
);
18
}
19
}
20
21
function App() {
22
return (
23
<div className="App">
24
<Lottery n={9}/>
25
<h1>Hello CodeSandbox</h1>
26
<h2>Start editing to see some magic happen!</h2>
27
</div>
28
);
29
}
30
31
ReactDOM.render(
32
<App />,
33
root
34
)
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
3
4
<div id="root"></div>