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

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!

Making Things Dynamic with Redux

Learn to make things more dynamic by adding counters using Redux, combining reducers, and creating a dynamic structure.

07:35

Creating a Reducer for the Checkboard

Learn how to create a reducer for the checkboard and integrate multiple reducers using the combined reducer function.

08:11

Deep Freezing Objects in Redux

Learn why and how to use deep freezing in Redux to prevent state mutation and ensure immutability in your application.

10:53

Dynamically Building a Board Component in React

Learn how to dynamically create a board component using React, including setup, CSS styling, and rendering elements.

11:13

Completing the Dynamic Board in React

Finish building the dynamic board component in React, including color flipping and rendering enhancements.

03:28