Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Loop Based Rendering | Controlling Data & Content
Introduction to React
course content

Conteúdo do Curso

Introduction to React

Introduction to React

1. Getting Started: ES6
2. Getting Started: JSX
3. React in Practice
4. React Components
5. Controlling Data & Content
6. Hooks
7. React Router

bookLoop Based Rendering

We can use JavaScript loops to easily render multiple components and perform other repetitive tasks without needing to repeat the code.

For example, we can create multiple instances of a component and store it in an array that can be directly rendered using the embed method.

Code explanation

In the index.js, there is a component called Grid, which contains the main <div> element having an id grid.

There's an empty array called blocks in the function, which is then populated with Block components using nested for loops. Each Block component appended to the array has a color value, either light blue or empty string - indicating the default color(white).

This color is assigned based on the condition (i + j) % 2 == 0, which ensures that each consecutive block has a different color.

In index.css, we set the display of the #grid object to grid, and using the repeat() function, we create 18 rows and 18 columns, each having a width/height of 27px. This arranges all those Block components (which are basically <div> elements) into a neat 18x18 grid structure).

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 3
some-alt