How to list item in rows of 4 ReactJS
Understanding the Challenge
Imagine you have a box full of different colored balls and you want to display them in rows of four. This is similar to what we are trying to achieve in this ReactJS tutorial. We will be creating a list of items and displaying them in rows of four.
Start with the Basics: Creating a List in ReactJS
Our first step in this journey is to create a simple list in ReactJS. A list in ReactJS is simply a JavaScript array displayed using JSX. JSX is a syntax extension for JavaScript, which allows us to write HTML elements in our JavaScript code. It's like a bridge between JavaScript and HTML.
Here's a simple example of creating a list in ReactJS:
import React from 'react';
class App extends React.Component {
render() {
const colors = ['Red', 'Blue', 'Green', 'Yellow'];
return (
<ul>
{colors.map((color) => <li>{color}</li>)}
</ul>
);
}
}
export default App;
In the example above, we have an array of colors (our different colored balls) and we use the map()
function to create a new array of JSX elements (our HTML list).
Breaking Down the Challenge: Rows of Four
Now that we have our list, how do we arrange it in rows of four? This is where CSS comes into play. We can use CSS Grid to easily create a flexible grid layout. Imagine drawing a 4x4 grid on a piece of paper and placing your colored balls in each square. That's what we're going to do with our list items.
Here's how you can apply CSS Grid to our list:
import React from 'react';
import './App.css';
class App extends React.Component {
render() {
const colors = ['Red', 'Blue', 'Green', 'Yellow', 'Purple', 'Pink', 'Orange', 'Black'];
return (
<div className="grid">
{colors.map((color, index) => <div key={index} className="cell">{color}</div>)}
</div>
);
}
}
export default App;
In the CSS file App.css
, we'll add the following CSS rules:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
}
.cell {
border: 1px solid #000;
padding: 10px;
}
In our JSX code, we're wrapping our list in a div
with a class of grid
, and each item in our list is also a div
with a class of cell
. In our CSS, we're using display: grid
to create a grid layout, grid-template-columns: repeat(4, 1fr)
to create four equal-width columns, and grid-gap: 10px
to add some spacing between our items.
The repeat(4, 1fr)
in grid-template-columns
is like saying "make four columns, each taking up an equal fraction of the available space".
Conclusion
And voila! You've just displayed a list in rows of four in ReactJS using CSS Grid. It's like arranging your colored balls in a 4x4 grid on a piece of paper. The key takeaway here is that you can create complex layouts with simple and intuitive CSS rules.
Remember, in programming, a complex problem is often best solved by breaking it down into smaller, more manageable parts. We first created a list, then we arranged the list into rows of four.
Keep practicing and experimenting with different CSS Grid configurations - maybe try arranging your list in rows of three or five next time. Happy coding!