02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Dynamically Building a Board Component in React

Tutorial: Dynamically Building a Board Component in React

In this tutorial, we'll walk through how to dynamically create a board component in React. We'll be using a combination of JSX, React components, and CSS styling to achieve a structured board. By the end of this tutorial, you'll understand how to build out a dynamic grid-like board, apply CSS for styling, and update component states effectively.

Step 1: Setting Up Your Components

The first step in creating our dynamic board is to set up the necessary components in React. We'll start by creating the following files:

  • Board.js
  • Board.css

To do this, navigate to your components folder and create Board.js for the board logic and Board.css for the corresponding styling. Following the conventions used in Create React App, make sure both files are properly structured.

Step 2: Initializing the Board Component

Inside Board.js, start by importing the necessary React libraries and styles.

import React from 'react';
import './Board.css';

We then create a presentational component that renders the board. Initially, you can copy the content from an existing component like Counter and adjust it accordingly.

For example, here's a simple version of the Board component that serves as a starting point:

const Board = () => {
  return (
    <div className="board">
      This is the board.
    </div>
  );
};

export default Board;

Next, update App.js to import and use the Board component. This will add our board component to the app's presentational layer.

Step 3: Adding Dynamic Logic to the Board

We want to dynamically create the board using values for width and height that come from props. To do that, let's add some logic to the Board component to calculate and render rows and columns.

First, extract the width and height values from props and create empty arrays to represent rows and boxes:

const Board = ({ counters }) => {
  const width = counters[0] || 0;
  const height = counters[1] || 0;

  let lines = [];

  for (let i = 0; i < height; i++) {
    let boxes = [];
    for (let j = 0; j < width; j++) {
      boxes.push(
        <div className="board-box" key={`box-${i}-${j}`}></div>
      );
    }
    lines.push(
      <div className="board-line" key={`line-${i}`}>{boxes}</div>
    );
  }

  return (
    <div className="board">
      {lines}
    </div>
  );
};

In this code, we're iterating over height and width to generate each row (board-line) and each cell (board-box). Each box is assigned a unique key to ensure React can correctly handle re-renders.

Step 4: Styling the Board Component

Now that the board component is rendering, we need to add CSS styling to make it look like a board. Open Board.css and add styles for .board, .board-line, and .board-box.

Here's an example:

.board {
  display: inline-block;
  background-color: gray;
}

.board-line {
  display: flex;
}

.board-box {
  width: 50px;
  height: 50px;
  display: inline-block;
  background-color: red;
  border: 1px solid black;
}

The .board class is used to define the board container, while .board-line ensures that each row is displayed horizontally. The .board-box class creates individual cells within the board, setting dimensions and background color.

Step 5: Integrating with Application State

To make the board dynamic, integrate it with your application's state. Pass the counters prop to the Board component from the app's state, allowing changes to width and height to automatically reflect in the rendered board.

Update App.js to dispatch the necessary props to Board:

<Board counters={store.getState().counters} />

Step 6: Testing Your Component

Make sure everything is wired up correctly by running your application. As you modify the counters that determine the board's width and height, the Board component should update accordingly. You can add console logs or debugging tools to ensure all props and state changes are properly handled.

If you run into issues like CSS not being applied or elements not showing, double-check your imports and ensure the CSS file is correctly linked to Board.js.

Video Transcript

00:00Alrighty, time for us to get back into React,
00:02where we're going to be dynamically building out our board.
00:05So let's jump right into it.
00:07It's the first step of two steps.
00:09Let's go ahead and continue in our process
00:12of building actual the co actually the components itself.
00:15So to do that, I'm gonna go into my components folder
00:17and I'm gonna create a few new files.
00:20One, I'm gonna create our board itself, board js,
00:25and let's go ahead and also create, uh, a file for our css.
00:29So I'm just gonna call that board css
00:31and I'm gonna stick to the conventions that exist within
00:35re uh, create React app.
00:37So lemme just go ahead
00:38and I'm gonna go into my, um, counter itself,
00:42'cause I'm just gonna copy everything inside
00:44of our counter into our board itself
00:46and then delete everything that's irrelevant.
00:48So for one, we're not gonna access the store directly.
00:51We have your, uh, presentational layer component.
00:55So it's literally just a rendering method.
00:58And in our rendering method, what I'm gonna go ahead and do,
01:01and let me just delete everything here as well.
01:03I'm just gonna type here, this is the board
01:06and we'll go ahead and first integrate it into
01:09our application itself.
01:10So to do that, I'm gonna go into my app itself,
01:13and I've already done that in between
01:15and just import our board itself
01:18and add in our board to our presentational layer.
01:22Another thing that I've done in
01:24between was change the class name of our, uh,
01:27diviv up here from App info.
01:29Uh, from app, uh, I intro, I think it was to app info.
01:33And I also, you don't have to do it, but I did it
01:35and also updated inside of the app.
01:38CSS itself changed it to app info.
01:41Last but not least, I changed our, uh, float
01:44or added a float inside to make it float left.
01:47So literally what I've done is if we go,
01:49if you go into your app itself,
01:51those changes will basically make sure
01:53that this is gonna be added to the side.
01:55That way our board itself will be nicely positioned in the
01:58center as soon as we start.
02:01With that said, let's go ahead
02:02and start implementing our board creation itself.
02:05Now, what I'm gonna do is I'm gonna, now
02:07that we have our app sending in all of the data,
02:10everything in our store, I could go into my board itself
02:13and literally fetch from the board itself the different
02:17variables and different information that I care about.
02:20So, for one is I wanna fetch our counters
02:22and our board itself from our this
02:25props data.
02:29Next, I'm gonna go ahead and note.
02:31So I'm going to fetch our width,
02:32and our width is literally going to be our counters
02:36in position zero.
02:38And if there's nothing there, then let's just make sure
02:40that it will return zero no matter what.
02:43And we want to have our height itself, which is well
02:45as well, counters position one.
02:48If there's nothing there, we want it to be set to zero.
02:53Okay? So the next step that I want to do is I want
02:56to go ahead and literally cycle through
02:59and create basically a variable
03:01that I'm gonna place inside of here.
03:03So this is the board.
03:04Maybe we'll have, let's just put here a brake vine
03:07and inside of here we'll just po position our variables.
03:10So let's call this, let's say, uh,
03:12let's call this out just just for the sake
03:15of creating some sort of a variable name.
03:17So I'm just gonna go ahead here
03:18and I'm gonna create a new out object.
03:20And our out object is actually gonna be a new array.
03:24Now, we could even set the actual width of that array
03:27because we know how many lines in this case, you know,
03:30let's even call this lines.
03:32So our lines itself was literally gonna be, uh,
03:35the based on our, based on our,
03:41well, actually it should be our height, right?
03:42Because there's height, right?
03:44All right, so this is gonna be our lines,
03:47lines, lines.
03:50Once we have our array lines,
03:51let's go ahead and start creating it.
03:53So first of all, I'm gonna loop through our, uh, array,
03:57really, uh, to create it starting from zero
04:00and basically going up by one each time.
04:03And I'm gonna go ahead here
04:04and just make sure that my eye is smaller than lines
04:07of length, but technically
04:09what I could do is also just use the height,
04:10which is a constant, which is even better
04:13and progress further
04:17in every single line.
04:19What do we wanna do?
04:20First of all, in every single line, what we really want
04:22to do is we wanna basically create a new line, uh, the,
04:26basically the internals of that line itself.
04:29So I could go ahead here and maybe go ahead and create here.
04:32Um, uh, let's call this boxes
04:36and it's gonna be a new array.
04:38And that array will be based on the width itself.
04:44Okay? So we have our box itself,
04:46and our box itself will actually have inside of it that, uh,
04:50the amount of elements that would exist within a box.
04:54The next step is we wanna loop through them as well. Again.
04:57So I'm gonna go ahead here
04:58and create, uh, I don't wanna create the new J
05:00because I actually wanna refer to it.
05:02You know, let's start from that. So let's j uh, for now,
05:04let's, we will have a localized variable here
05:06that is only available within the four loop itself.
05:09That will cycle through the width itself
05:12and grow our J each time our cycle goes through.
05:16Now, what do we wanna do in this scenario?
05:19Step number one is we wanna create that box.
05:21So let's go ahead and go into boxes into position J.
05:24And literally I'm just gonna assign to it a JSX code,
05:28which will be a diviv class name,
05:32and let's give it a name.
05:33So I'm gonna give it a name of board box,
05:38and I also wanna make sure that there's a key inside
05:41as we spoke before, it needs
05:42to be unique per the context that it's within.
05:45So I'm gonna create that unique box, unique key, okay?
05:49So that's the first step for our diviv layer itself,
05:52we have here a diviv layer.
05:55And in this div layer, literally that's, well,
05:57that's all we'll do for this point in time.
06:01Excuse me. Now, there's a few more things we wanna do inside
06:05of this loop, but let's go ahead and first just integrate it
06:08and implement it into our lines itself.
06:10So after we've created all the boxes that are relevant to
06:13that specific line,
06:15I'm gonna go into my lines into position I
06:18and create here a new wrapper.
06:20So I'm gonna call this Diviv class name
06:23and our wrapper, let's call this board line.
06:27And again, it needs a key
06:28because it's gonna be put in dynamically
06:32and we wanna position inside of this diviv,
06:35we wanna position our boxes
06:37and we also wanna make sure
06:38that there's no extra line spaces.
06:40So I would even just delete everything here
06:42and just literally just make sure that it's, uh, make sure
06:46that it's literally all snug together without any spaces
06:49or anything in between the boxes.
06:52Last but not least, well it's far from last.
06:55We wanna make sure that we're actually placing
06:57this inside of our lines.
06:59Now, to be able to really test this out, we really need
07:01to put some content within our board.
07:04So to do that, I'm gonna go ahead in my board itself.
07:07And let's just go ahead and put your sum, sum logic.
07:09So first of all, for our board itself, let's go ahead
07:13and display it in line.
07:19Uh, we also wanna have a background for it
07:21so we could see the changes in the colors.
07:22So let's say we'll set it to gray and let's go ahead
07:26and o to create our
07:28board board line.
07:32We don't really know if we really need anything inside
07:35of it, but let's just create our board box now
07:39for the box we definitely do want.
07:40So we wanna make sure that that displays oto in line.
07:45And we also wanna make sure that it has a set width
07:47and height 'cause we're not setting that anywhere else.
07:50And let's do a height as well of let's say 50 px.
07:54And let's just put our color just so we could see something.
07:56So background red for now until we, uh,
07:59integrate our color logic from the board itself.
08:02So let's go ahead and see if it actually works
08:04and if we have any errors.
08:05So first of all, we have your small error warning board is
08:08assigned a value but never used, no unused variables.
08:11So let's see where that is actually happening.
08:13We have your variable called board
08:15that is actually not being used inside of our board itself.
08:19So in our board itself, it seems
08:21that we have a variable called board,
08:23but we're actually not using it.
08:25Okay, that's fair enough. We're gonna use this soon.
08:27So that's not really an error,
08:29it's just a warning to let us know.
08:30So that's fine. Now in our board logic itself,
08:34as we add elements into our board,
08:36we should be getting here something,
08:38but we're not getting anything here.
08:40So let me just go ahead and also put inside
08:42of our board itself.
08:44Let's set here some sort of a, a, a trace here.
08:47So console log, just to see what's actually going on.
08:50So this will be X for actually adding items.
08:53And let's go ahead
08:54and take, take another peek and see what happens.
08:57So as we were adding things, we're cycling
09:00through adding more items.
09:02Now we're starting to get those excess 40 times.
09:04So it is going through it, but let's go ahead
09:06and look into the inspect it
09:07to see if it's actually being added onto our display.
09:10So if I go into our react,
09:14it's creating it inside of our app itself.
09:18We wanna, we wanna find
09:20our, well, that's a good point.
09:23I don't really see it. Do I counter, counter, counter?
09:26I see your counter again, which is not really
09:28what we wanted to have.
09:30So let's see what's going on there.
09:31So literally we're seeing that in our app itself,
09:34our board is actually a counter.
09:37No, that doesn't make any sense.
09:38Uh, but in our app itself, app in our board itself,
09:44we are outputting just an a div here.
09:47And from copy pasting, I actually forgot
09:50to change the name to border.
09:51Alright, so that's to board. That is board.
09:56So that's good. Let's, uh, for, by the way,
09:59that's not our problem, but it is at least, at least we,
10:02we fixed a small problem
10:03that would probably be completely invisible to us.
10:05Okay, so now we have our board.
10:07And in our board we definitely have your nothing right now,
10:10but if we start adding items, we should start seeing lines
10:13and, and, and elements.
10:15So there you go, we have your lines
10:16and that's exactly what we want to happen.
10:18But we're not actually getting any CSS, which means
10:21that our CSS is not combined to our board, which is
10:24because we never actually imported it.
10:26So in the conventions of, uh, create react app,
10:30I'm gonna go ahead and import our css, which is in board
10:35css, save it, and go back into my application itself.
10:39And now we should, whenever we're gonna add two items
10:42or more and actually start adding more than one,
10:44we should now have your um, eight in 1, 8, 2, 3, 4,
10:49which is exactly, ooh, look at that.
10:51Interesting, uh, exactly what we wanted to to happen.
10:54So we'll fix that CSS problem as well.
10:57Uh, so let's see in the next lecture,
10:58as we continue building out the board in this section,
11:02we in lecture, we started out building our checkered board.
11:06We're not done yet. We have one more lecture to go.
11:08And in the next lecture we're gonna connect everything
11:11together to create our dynamic board.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!