Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to align table in ReactJS

Getting Started with Tables in ReactJS

So, you've started your journey in programming, and you've come across an important step - creating tables in ReactJS. Don't worry if it sounds daunting. We'll break it down together, step by step, just like assembling a puzzle.

What is a Table in Programming?

Think of a table in programming as a spreadsheet. It's a way of organizing information into rows and columns, just like you would do in a Microsoft Excel sheet. In the context of a webpage, a table can display data from a database or allow users to input data in a structured format.

Creating a Basic Table in ReactJS

Now, let's dive into the code. First, we will create a basic table in ReactJS. You don't need to understand everything at this point. Try to get a feel for the structure of the code. We'll discuss each part in detail later.

import React from 'react';

const BasicTable = () => {
    return (
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>John</td>
                    <td>25</td>
                </tr>
                <tr>
                    <td>Jane</td>
                    <td>30</td>
                </tr>
            </tbody>
        </table>
    )
};

export default BasicTable;

Understanding the Code

Let's look at the code closely. We start by importing React. This gives us access to the React library, which we need to create a React component. Our table will be one such component.

Next, we declare a function, BasicTable. This function, when called, will return our table. Inside this function, we use JSX, a syntax extension for JavaScript, to define the structure of our table. JSX might remind you of HTML, and for a good reason - it's meant to be a way to write HTML in your JavaScript code.

Our table contains a header (<thead>) and a body (<tbody>). The header provides labels for each column. The body contains rows (<tr>), which in turn contain cells (<td>). Each cell represents a piece of data.

Finally, we make the BasicTable function available for use in other parts of our codebase by exporting it.

Making the Table Dynamic

Our table is static at this point, but we can make it dynamic, meaning it can display different sets of data. To do this, we introduce a prop, data, to our BasicTable function. A prop is like a parameter for a function - it allows us to pass information from one component to another.

const BasicTable = ({data}) => {
    return (
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                {data.map((row, index) => (
                    <tr key={index}>
                        <td>{row.name}</td>
                        <td>{row.age}</td>
                    </tr>
                ))}
            </tbody>
        </table>
    )
};

Here, we use the map function to create a new <tr> (table row) for each item in data. Each item (which we call row) is an object with properties name and age, which we display in their respective <td> (table data) cells.

Aligning the Table

To align the table, we will use CSS. CSS stands for Cascading Style Sheets and is used for styling HTML elements. In the context of our React component, we can use it to align our table to the center of the page.

First, we need to add a className to our table. A className is like a label we can attach to an HTML element to apply specific styles to it.

<table className="center">

Then, in our CSS file, we add the following code:

.center {
    margin: auto;
    width: 50%;
    padding: 10px;
}

This code block tells the browser to automatically adjust the margins of any element with the class center (which is our table), effectively centering it. The width of the table is set to 50% of the parent element, and a padding of 10px is added for good measure.

Wrapping Up

You've just taken your first steps into creating and aligning tables in ReactJS. It might feel a bit like learning a new language, and that's okay. Remember, fluency comes with practice. You’ve built a dynamic table that can display any data you pass to it, and you've also learned how to center this table on the page.

Go ahead and try it out. Experiment with different data. Play around with the CSS. Pretty soon, you'll be handling tables in ReactJS like a pro!

Every journey begins with a single step. This was yours in handling tables in ReactJS. Now, you're equipped to take the next one. Go forth, explore, and remember: the only limit is your imagination. Enjoy the journey!