I have a task, but i couldnt get the solution for it in reactjs. I would like to show the result in two columns like: So every next element should be added in the second column, but the first column should be limited to 2 rows.
JavaScript
x
4
1
item 1 | item 4
2
item 2 | item 5
3
| item 6
4
When i loop through the array the columns are getting divided into equal order, but as you see in the diagram i need to split them in 2:3 ratio. I am giving the sandbox link here on what i have tried so far.
JavaScript
1
52
52
1
// Example class component
2
class Thingy extends React.Component {
3
render() {
4
const secondaryNav = [
5
{
6
title: "Games",
7
name: ["Roadrash", "Prince of Persia", "Counter Strike"]
8
},
9
{
10
title: "Resources",
11
name: ["Images", "Videos", "E-books"]
12
},
13
{
14
title: "Shop",
15
name: ["Shop now"]
16
},
17
{
18
title: "Account",
19
name: ["Log In", "Register"]
20
},
21
{
22
title: "Support",
23
name: ["Email", "Call Us", "Get a callback"]
24
}
25
];
26
return (
27
<div className="App">
28
<div className="container">
29
<div className="row">
30
{secondaryNav.map((item, i) => {
31
return (
32
<div className="col-lg-6 col-md-6">
33
<h3>{ item.title }</h3>
34
<ul>
35
{item.name.map((items, i) => {
36
return <li>{items}</li>
37
})}
38
</ul>
39
</div>)
40
})}
41
</div>
42
</div>
43
</div>
44
)
45
}
46
}
47
48
// Render it
49
ReactDOM.render(
50
<Thingy title="I'm the thingy" />,
51
document.getElementById("react")
52
);
JavaScript
1
7
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
<div id="react"></div>
4
5
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
6
integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"
7
crossorigin="anonymous">
Advertisement
Answer
You need to add the css on it and also you could slice your array as you like.
this is the piece of the code
JavaScript
1
29
29
1
<div className="row">
2
{secondaryNavData.slice(0, 2).map((item, i) => {
3
return (
4
<div className="col-lg-6 col-md-6">
5
<h3>{item.title}</h3>
6
<ul>
7
{item.name.map((items, i) => {
8
return <li>{items}</li>;
9
})}
10
</ul>
11
</div>
12
);
13
})}
14
</div>
15
<div className="row-2">
16
{secondaryNavData.slice(2, 5).map((item, i) => {
17
return (
18
<div className="col-lg-6 col-md-6">
19
<h3>{item.title}</h3>
20
<ul>
21
{item.name.map((items, i) => {
22
return <li>{items}</li>;
23
})}
24
</ul>
25
</div>
26
);
27
})}
28
</div>
29
Try this codesandbox based on yours for the full code with the css https://codesandbox.io/s/elegant-grass-nmeux