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

Completing the Dynamic Board in React

Tutorial: Completing the Dynamic Board in React

In this tutorial, we will continue building the dynamic board component in React. We'll leverage Redux to manage our state and make sure the visual layer dynamically updates the checkered board based on data from the store. Let's get started!

Step 1: Setting Up CSS

First, we need to fix the CSS problem by adding the necessary class names. Ensure that the class name is set correctly to align with our component structure. This will make sure the board has the intended styles and appearance.

Add a class name to the board component to style it properly:

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

This change ensures that the board lines up nicely and that the background color is applied as expected.

Step 2: Dynamic Board Rendering

We want each item in our board to have a different color based on the colors stored in our Redux store. This will create the classic checkered board effect. To achieve this, we'll be flipping the colors dynamically.

Adding Color Flipping Logic

To alternate the colors, we need to add a boolean variable to keep track of which color to use. We'll call this variable isColorOne and start by setting it to true.

Here's the logic to flip the color:

let isColorOne = true;

for (let i = 0; i < height; i++) {
  let boxes = [];
  for (let j = 0; j < width; j++) {
    boxes.push(
      <div
        key={`box-${i}-${j}`}
        className="board-box"
        style={{ background: isColorOne ? board.color1 : board.color2 }}
      />
    );
    isColorOne = !isColorOne;
  }
  lines.push(
    <div key={`line-${i}`} className="board-line">
      {boxes}
    </div>
  );
  // Flip the starting color if the width is even
  if (width % 2 === 0) {
    isColorOne = !isColorOne;
  }
}

In this code, we:

  • Initialize isColorOne to true to start with the first color.
  • Use a nested loop to iterate over each row (height) and column (width) of the board.
  • Flip the color each time a box is added using isColorOne = !isColorOne.
  • After each row, if the width is even, we flip the starting color for the next row to maintain the checkered pattern.

Step 3: Applying Inline Styles

To apply the color dynamically, we use the style prop in JSX to set the background color of each box:

<div
  key={`box-${i}-${j}`}
  className="board-box"
  style={{ background: isColorOne ? board.color1 : board.color2 }}
/>

This allows us to control the color of each box based on the value of isColorOne. If isColorOne is true, we use board.color1; otherwise, we use board.color2.

Step 4: Completing the Board Component

Finally, we render all the rows (lines) inside the board component:

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

This will display the entire board with alternating colors, giving us the checkered board effect.

Step 5: Importing CSS

Don't forget to import the CSS file to apply styles to your components:

import './Board.css';

This will ensure that all the styles, such as the display properties and background colors, are applied correctly.

Summary

In this tutorial, we completed the dynamic board component in React. We:

  1. Fixed the CSS issues by adding the necessary class names.
  2. Added color flipping logic to alternate the colors dynamically.
  3. Used inline styles to apply colors based on the board state.
  4. Rendered the entire board with alternating colors to create a checkered effect.

In the next section, we'll dive deeper into integrating React with Redux and understand how to manage complex state interactions effectively.

Happy coding!

Video Transcript

00:01Alrightyy.
00:01So it's time for us to complete creating our dynamic board,
00:04and we're gonna obviously be in React.
00:06We're gonna use our Redux data
00:08and we're gonna make sure
00:09that our React visual layer will automatically
00:12and dynamically change our checkered board based on the data
00:15that's coming through it.
00:16So let's complete building out our board.
00:20Alright, so our next step that we wanna do is,
00:22and by the way, to, to fix our CSS problem, I just had to,
00:26uh, add my class name, which I haven't done that, which, uh,
00:29would make that whole line, um, board, which would make
00:34that whole line basically, uh, adhere to these properties
00:36that we added, that it'll be an in line
00:38and that would be gray.
00:39But let's continue down our path.
00:41So we have our, our, our element inside.
00:43The next step is we want each item in our board
00:46to have a different color based on the colors
00:48that are stored inside of our store, inside
00:51of our board area, of our store.
00:53So to do that, what I'm gonna do is I wanna flip that color.
00:57So to be able to do that, I'm literally just gonna
00:59add another array.
01:00Uh, another bullion to be honest.
01:02Uh, honest, why honest, what was I lying until now?
01:05So is color one true? We'll start from that.
01:08And what I want to do is I want basically wanna flip colors.
01:11Basically adding in a color based on what our
01:15is color one is,
01:16and literally what I wanna do every time I actually finish,
01:19uh, adding a, a color in the loop, I want
01:21to go into my is color one
01:23and literally just flip it to be the opposite.
01:26Oops. The opposite of is color one.
01:30Okay. And now in the color itself,
01:32I'm gonna actually add this in through a style property
01:36and I'm gonna send in an object into our JSX where
01:40that object will have a background.
01:42And our background is gonna be either or.
01:44If is color one, then we want to take our board color one,
01:50and if not, we want to take our board color two.
01:56So we're flipping around our color.
01:57Now, if we run our application again,
02:00we should start seeing different colors,
02:02but it's not going
02:03to work a hundred percent the way we would want
02:05it to work all the time.
02:07For one, whenever the value is odd, then yes, fine,
02:11no problem, everything is working.
02:13But whenever we have an, uh, even value,
02:15then our values are not gonna be
02:17exactly as we want them to be.
02:18So one more thing that we're gonna need
02:20to do is every time we finish our loop,
02:22we're gonna ask if our j which basically means that we need
02:26to reference to our J.
02:27So let me just push our J definition one step up to the loop
02:32above, and I'm gonna ask if our J divided by two, uh, the,
02:36the leftover from division by two equals zero.
02:39Then I'm just gonna go ahead
02:41and flip our is one, one more time.
02:43So this is literally for just scenarios where our
02:48division is equal and
02:50whenever our division is equal, I'm gonna flip it
02:51already in that line itself.
02:53So let's go ahead and save that.
02:54And if I go back into my application,
02:56we should now have a fully,
02:59Fully lovely board
03:00that if I have four lines it will, it will be great.
03:04If it's five, it'll be great.
03:06If it's an eight by eight, it will be grand as well.
03:11So there you go. Now obviously we have your extra counters
03:13that we don't need and that's something
03:14you probably would wanna address.
03:16Hope you weren't board, but we're done with that board.
03:19If you were. In any case, we're moving on
03:21to our next section and in our next chapter we're gonna
03:24integrate react, redux, and understand where and how
03:27and why it's used.

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!